简体   繁体   中英

PHP Rewrite the URL with .htaccess

I'm trying to rewrite the URL using .htaccess . These all the things I want to do with the rewriting

  1. Remove the file extension from the URL
  2. Pass the parameter through URL and GET it in the file

My current .htaccess the file is as below

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^(.*)$ works?v=$1 [NC,L]

This will rewrite and remove the file extension ( http://example.com/file.php to http://example.com/file )

I want to send some data (maybe a string) to the page

let's say http://example.com/file2.php?v=something

I want to rewrite it as http://example.com/file2/something and get the value in the file

I tried the following methods

Method 1

Method 2

If I try Method 1 and Method 2 (one after another), it's throwing following error

[Sat Aug 03 12:13:12.881757 2019] [core:alert] [pid 23832:tid 1912] [client ::1:62842] path/to/htaccess/file/.htaccess: RewriteRule: bad flag delimiters

I tried This Method to prevent that error then It threw me below error

[Sat Aug 03 13:14:02.121514 2019] [core:error] [pid 23832:tid 1916] (20023)The given path was above the root path: [client ::1:51899] AH00127: Cannot map GET /path/to/my/file2?v=something.php HTTP/1.1 to file

I think it because .htaccess is rewriting the value as .php file.

How can I hide file extension and Get the value at the same time?

I think this is what you are looking for:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([^/]+)/?$ /$1.php [END]

RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/?$
RewriteRule ^ /%1.php?v=$2 [END]

This certainly can be refined further for your specific case. You question is a bit vague in a few points. But above example should point you into the right direction...

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

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