简体   繁体   中英

How to hide the name of the php file in url

So I have this blog where I have two php files one serves as an index to all my blog posts

www.adityasastry.in/view.php?cat=1

the cat value stands for category I have three categories, programming, embedded systems and rant.

I want to change this something like www.adityasastry.in/1 should translate to www.adityasastry.in/view.php?cat=1

and another in the same directory(I can move this to a different directory if I want) which lets me view a particular blog post.

www.adityasastry.in/viewer.php?cno=32

I have indexed each of my blog posts with a number.

Ideally I want this to translate to www.adityasastry.in/1/32 if 32 is a blog post belonging to 1 category.

Please don't ask me what I have done cause I am not even sure this could be accomplished with PHP alone on a shared host !! I just selected couple of tags that I think are relavant to this.

http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708 - This is a good tutorial.

Basically-

Find the httpd.conf file for your server and uncomment the following line by removing the # :

# LoadModule rewrite_module modules/mod\_rewrite.so 

Create a file called .htaccess (no name, extension .htaccess) and open it in your code editor of choice and enter the following:

RewriteEngine on

Which enables the rewrite engine obviously. From there you can make rewrite declarations by writing RewriteRule followed by a regular expression that matches the URL you are trying to catch, followed by the URL you want to be "redirected" to, using $1, $2, ... , $n etc to match the bracketed parts of the regex respective of the order they appear in it.

RewriteEngine on

RewriteRule ^/view/([0-9]+)/?$ /view.php?cat=$1

Save that file in the root of your website. Would take http://adityasastry.in/view/666 and "redirect" to http://adityasastry.in/view.php?cat=666 . I would suggest reading the article I mentioned above as it covers this practice in far greater detail.

If I understand you correctly, you could put this in your htaccess file. (assumes Apache)

DirectoryIndex view.php

See this Q&A for reference

If your site automatically goes to index.php, then you might skip this and then in index.php load whatever page/file you want with a conditional include.

None of what I am saying is intended to dispute the re-write answers, just giving alternatives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM