htaccess RewriteRule issue using GET - .htaccess

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.

Related

too many redirects with .htaccess and a very simple configuration

Good day all.
I'm doing a simple dashboard o a site, I've set up a very simple htaccess to handle some URLs:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#routing:
RewriteRule ^login/?$ login.php [L]
RewriteRule ^page/([0-9]+)/?$ /index.php?page=$1 [QSA,NC,L]
#errors:
ErrorDocument 404 /404.php
As far as I know this should be quite straight forward, but:
going on /page/1234 URL works fine (as well as any other number used, so the pattern is working).
going on www.example.com/login OR www.example.com/login/ is causing:
1) too many redirects error if the "errorDocument" line is on top of everything
2) a /404.php redirect if the "errorDocument" is at the bottom of the htaccess.
while, I can access directly /login.php without any problems.
i've done some tests but I can figure out what is going wrong, I've also tested the file with this tool:
Converting my comments to answer.
Looks like you have MultiViews option turned on and getting this unexpected behavior.
Options -MultiViews
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 try to resolve it and serve /file.php.

Redirect url internally but keep entered URL

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

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.

Rewrite 1 level of url directory

I'm looking to rewrite the first directory of a url string and have the rest of the request still work.
Eg: I want it so when a user clicks the link for : /products/category/item.php
it actually grabs the file of : /shop/category/item.php But still shows as /products/category/item.php as the URL
This will be dynamic so it should be something like /products/$ /shop/$1 I'm guessing.
You do not need mod_rewrite. when to avoid mod_rewrite.
Mapping url directories to file directories is a basic functionnality of Apache handled by the mode mod_alias (which is quite certainly already present for you).
So basically you have the Alias and AliasMatch directives. In your case the first one is enough:
Alias /products/ /path/to/web/document/root/shop/
The mapping is done only server-side so the url seen by the end user is never modified.
Try this:
RewriteEngine On
RewriteRule ^products/(.*) shop/$1 [L]
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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/(.+)$ /shop/$1 [L,NC]

Change URL but stay on the same page using htaccess

I have a URL:
www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space
I want to stay on the same page above but simply display the URL in the address bar like this:
www.example.com/property-listings/united-states/colorado/denver/office-space
How do I accomplish this using htaccess and rewrite rules?
If I understood right, try writing a rule like this one:
RewriteEngine on
RewriteRule property-listings/united-states/colorado/denver/office-space http://www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space [L]
OK. You didn't supply a pattern or mentioned there was any, so I have to guess the pattern is up to /denver/ subdirectory. Try this:
RewriteEngine on
RewriteRule ^(property-listings/united-states/colorado/denver/)(office-space)/?$ $1denver-co-$2 [L]
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 /
RewriteRule ^(property-listings/united-states/colorado)/(denver)/(office-space)/?$ $1/$2/$2-co-$3 [L,NC]

Resources