.htaccess mod_rewrite won't skip RewriteRule with [S] - .htaccess

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php

First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps

Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

Related

RewriteRule for 'pretty links' not working

I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

htaccess: Rewrite if url is www.domain.com/some-string/xxx but not www.domain.com/some-string-is-here

My htaccess looks like the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^some-string/$ [NC]
RewriteRule ^some-string(.*)$ http://www.domain.com$1 [R=301,L]
So all requests from www.domain.com/some-string/xxx should be redirected to www.domain.com/xxx (from subfolder to main domain). Now I had the problem that an article contained exactly the some-string part in his name e.g. www.domain.com/some-string-is-here. The result is that the article is not found.
How do I have to adapt the redirect?
Making the RewriteRule a bit more specific should help; in that case the RewriteCond is not necessary. As only requests starting with some-string/ (including the /) should be rewritten, appending that / to the rule does the trick for me:
RewriteEngine On
RewriteBase /
RewriteRule ^some-string/(.*)$ http://www.domain.com/$1 [R=301,L]
If you also want to rewrite www.domain.com/some-string (and only this and not www.domain.com/some-string-more), you can try instead:
RewriteRule ^some-string(/.*)?$ http://www.domain.com$1 [R=301,L]

Htaccess url rewrite not working correctly

I have an issue with url rewriting, here's what I have so far:
Options +FollowSymlinks
RewriteEngine on
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC]
Basically, if the url is mysite.com/users/username I want it to use profile.php, then for anything else, to go to main.php.
However, with that code it doesn't happen as expected, it always uses main.php. Is there anyway you can use if and elses in htaccess, or something similar?
You're missing L flag from 2nd and 3rd rules. Have your code like this:
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC,L,QSA]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC,L,QSA]

Resources