I'm trying to rewrite my url with .htaccess to beautify my urls but I'm getting page error. What surprises me is that it's working on my friend's machine using xampp and I'm using apache on my linux machine.
# Turn Rewrite Engine On
RewriteEngine On
# Rewrite for info.php?id=xx
RewriteRule ^photos/([0-9]+) info.php?id=$1 [NC,L]
# Rewrite for profile.php?user=xx
RewriteRule ^user/([0-9a-zA-Z_-]+) profile.php?user=$1 [NC,L]
# Rewrite for logout.php?redirec=xx
RewriteRule ^logout/([0-9a-zA-Z]+) logout.php?redirect=$1 [NC,L]
The most probable cause of error appears to be that mod_rewrite is not loaded. Check apache's httpd.conf file, and search for the line:
LoadModule rewrite_module modules/mod_rewrite.so
If there is a # before it, remove that and restart the server.
Related
I am using WAMP, the latest version with Apache 2.4.9. I was trying to beautify my URLs using mod_rewrite, that's preinstalled in my system. I uncommented the # before this line in httpd.conf:
#LoadModule rewrite_module modules/mod_rewrite.so
became
LoadModule rewrite_module modules/mod_rewrite.so
restarted the server, and wrote the following re-write rule in my .htaccess:
RewriteEngine on
RewriteRule ^localhost/testmod/(\W+).php$ localhost/testmod/$1
I saved it in a directory named testmode. Without modifications, URL of a file named test.php inside this folder looks like: localhost/testmode/test.php and I want to strip out the .php from the URL.
It doesn't change anything. The URL is same, localhost/testmode/test.php.
Am I making any mistake in the regexp or something else is wrong?
You have to turn that around:
RewriteEngine on
RewriteRule ^testmod/(\W+)$ testmod/$1.php [L]
Think of it like that: the incoming request is to http://localhost/testmod/test, that string has to be matched by the regex pattern. Then you internally map that the the physical script http://localhost/testmod/test.php in your file system.
Also make sure that the interpretation of such .htaccess style files is enabled in your http server.
And two general notes:
Since it is your system I assume you have control over the configuration of the http server. If so it always is better to place such rewriting rules (and similar stuff) in there instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and really slow the server down. They are only meant as a last fallback for those who do not have access to the configuration of the http server.
Note that you need a slight modification of the rule syntax if you decide to place the rules in the real server configuration:
RewriteRule ^/testmod/(\W+)$ /testmod/$1.php [L]
Your question is answered in the documentation of the apache module you want to use. You really should read that documentation. It is precise, complete and offers really good examples: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you have .htaccess inside testmod folder then these rules should work for you:
RewriteEngine On
RewriteBase /testmod/
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I'm trying to test some simple rewrite directives in .htaccess file on my test environment with Apache and PHP-FPM.
It seems that a simple rewrite rule works well for any type of file but not for *.php files.
This works :
RewriteEngine On
RewriteRule ^sandbox/video\.html?$ http://www.google.fr? [R,L]
But this not :
RewriteEngine On
RewriteRule ^sandbox/video\.php?$ http://www.google.fr? [R,L]
I run Apache 2.2 and PHP-FPM inside a VM on Debian
Could it be related ?
Thanks for your help
With PHP-FPM the .htaccess file is never read for php URL. You will need to add the rewrites to your main config.
I have these in my Apache vhost
RewriteEngine on
RewriteCond %{QUERY_STRING} ^highlight=WyJwb3J0YWwiLCJub3RpY2VzIiwicG9ydGFsIG5vdGljZXMiXQ==(&.*)?$ [NC]
RewriteRule ^component/k2/item/473-careers-portal-notices\.html$ /career-portal-notices.html?%1 [R=301,NE,NC,L]
However, when I hit site.com/component/k2/item/473-careers-portal-notices.html?highlight=WyJwb3J0YWwiLCJub3RpY2VzIiwicG9ydGFsIG5vdGljZXMiXQ== I do not get redirected to /carerr-portal-notices.html.
What do I need to change to make this redirection happen? And I've restarted Apache already.
This is a Joomla site. Apparently the .htaccess file that comes with Joomla can override any rewrite rules you place in the vhost.
Want im trying to acheive,
when url is hitting localhost/nap/ , internally it should call the url localhost/nap/inspect.do
To acheive this, Using Apache 2.2, following are the steps i followed,
Uncomment the line, LoadModule rewrite_module modules/mod_rewrite.so
Added he following line in httpd.cnf file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/nap/
RewriteRule ^(.*)$ /nap/inspect.do$1 [L]
restarted the apache server
But this configuration doesnt help me
You need only this in httpd.cnf file:
RewriteEngine On
RewriteRule ^/nap/$ /nap/inspect.do [L]
All requests with /localhost/nap/ will be redirected to run the file $DocumentRoot/nap/inspect.do script. L flag ignores all additional rewrite rules in the same file.
I have a problem trying redirect some user requests to my Apache server pointing to non existing files, using something like this:
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^/a/b/c.* /d/e$1 [L]
My intention is that, if some user requests file http://whatever.com/a/b/c/something, which doesn't exist, instead of sending the 404 error, rewrite the URL to get it from http://whatever.com/d/e/something, where I can be sure the file do exist.
I know there are a lot of topics on this, but I did very intense research, and my problem is no matter what combination I try, I can't seem to get the rewrite module to work..
Even with something like this :
RewriteRule ^(.*) /index.html [L]
That would presumably redirect EVERY petition to index.html , but it does nothing.
It is supposed to be loaded, as apache2ctl -M outputs rewrite_module as correctly loaded
Logging errors at debug level, I only see, users requesting non-existent files, and Apache answering '404 File does not exist' errors..
I'm the server admin so I'm not using .hcaccess, I edit directly the httpd.conf file, so there should be no overriding directives issues.
What am I missing?
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
...to redirect user to the index page.