Redirect url internally but keep entered URL - .htaccess

I have the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^p/(.*)$ /p.php?id=$1 [R=301,L]
Functionally speaking, the code above works. But the URL changes from p/X to p.php?id=X. I suspect the problem lies in the values between [], and I've tried different values, watching other questions on the same object, but no luck.
What am I doing wrong?

Remove R flag since that causes external redirection in browser.
Options +FollowSymLinks -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+p\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /p/%1? [R=302,L,NE]
RewriteRule ^p/(.+)$ /p.php?id=$1 [NC,QSA,L]
Also note disabling of MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Also added QSA flag. QSA (Query String Append) flag preserves existing query parameters while adding a new one.
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

Related

htaccess RewriteRule issue using GET

I'm struggling with this...
The RewriteRule looks like this:
RewriteRule ^media/viewMedia/([a-zA-Z0-9_-]+)$ /media/viewMedia.php?id=$1 [L]
RewriteRule ^media/viewMedia/([a-zA-Z0-9_-]+)/$ /media/viewMedia.php?id=$1 [L]
So, when the URL is /media/viewMedia/1, echo $_GET["id"] should result in 1, but output is blank? Of course if URL is /media/viewMedia.php?id=1, $_GET["id"] outputs 1.
You can use this rule with MultiViews turned off:
Options -MultiViews
RewriteEngine On
RewriteRule ^media/viewMedia/([\w-]+)/?$ /media/viewMedia.php?id=$1 [L,QSA,NC]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

htaccess make additional (hidden) request in background

I want a script on my server to excecute whenever there is a certain string found in the GET parameter. However I dont want the user to notice any of this as the server should serve the requested page like usual.
Is this possible?
RewriteCond %{REQUEST_URI} matchthis
# Make hidden request
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !^foo=bar(&|$) [NC]
RewriteRule ^matchthis/?$ %{REQUEST_URI}?foo=bar [L,QSA,NC]
This rule is adding a query parameter foo=bar if requested URI is /matchthis. This change will be hidden from the user since I'm not using R flag here hence it is an internal forward instead of an external redirect.

URLRewrite rule for a URL that only contains one value after the question mark

I'm working on redirecting a bunch of URLs to a new schema, basically from:
http://www.gamikaze.tv/?XHh-1Z56av0
to:
http://www.gamikaze.tv/#!/videos:XHh-1Z56av0
I tried to find examples of that, but all queries examples I've seen are using a key/value pair, and this only contains one value. Also, the URL doesn't fundamentaly changed - both URLs are using index.php. How could that be achieved using an .htaccess?
Try this code.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^([a-z0-9-]+)$ [NC]
RewriteRule ^$ /#!/videos:%1? [L,R=301,NE]

htaccess simple redirect doesnt work

Hi I need to redirect using htaccess every request which points at:
http://www.mydomain.com/index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387
to this url:
http://www.otherdomain.com
I've try to do it by:
redirect /index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387 http://www.otherdomain.com
But it doesnt work. So I need Your help.
Better to use mod_rewrite for this stuff.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_content&view=category&layout=blog&id=293&Itemid=387$
RewriteRule ^index\.php$ /? [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I think someone may need to extend my answer but you'll be looking to do something along the lines of:-
RewriteRule ^([^/]+)/? index.php?option=$1 [R=301,L]
The rule will require a regular expression so the server can compare the request.

htaccess - Redirect loop

Why is this causing a redirect loop?
Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/
RewriteRule !^[a-z]{2}/ /summary/1/document/en/ [L,R]
RewriteRule ^([a-z]{2})/ ../index.php?lang=$1&type=document
What I am trying to achieve is:
If there's no language specified, redirect to english:
Example:
website.com/summary/1/document --> website.com/summary/1/document/en/
website.com/summary/1/document/fr/ [no redirect]
And when a language is specified, rewrite internally to ../index.php with lang and type parameters.
From Flag L: Apache Docs: flag_l :
If you are using RewriteRule in either .htaccess files or in <Directory> sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.
Since, rewritten request is handed back to the URL parsing engine, after redirect from the first rewrite rule,
Try this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/
RewriteRule ![a-z]{2}/$ /summary/1/document/en/ [NC,L,R]
RewriteRule ^([a-z]{2})/$ ../index.php?lang=$1&type=document [L,QSA]

Resources