php - Htaccess as fake directories - .htaccess

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]

Related

mod_rewrite: url rewrite and redirect

I have the following URL:
http://example.com/pages/cms/impressum.php
and want to get an URL like this:
http://example.com/impressum
My rewrite Rule is:
RewriteEngine on
RewriteBase /
RewriteRule ^impressum$ /pages/cms/impressum.php [R,L]
The Problem is, I can open the url in both ways. I would like a forwarding from /pages/cms/impressum.php to /impressum. If I use [R=301], the second URL which I do not want works. I want to reverse this rule.
Use:
RewriteEngine On
RewriteBase /
# To externally redirect pages/cms/impressum.php to impressum
RewriteCond %{THE_REQUEST} \s/+pages/cms/impressum\.php[\s?] [NC]
RewriteRule ^ /impressum [R=301,L]
# To internally forward impressum to pages/cms/impressum.php
RewriteRule ^impressum/?$ pages/cms/impressum.php [L,NC]

Redirect dirty urls to a clean url

Alright so I have done the process of cleaning up my urls and everything is going good I put the following in my htaccess.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /l/index.php?mp3=$1&img=http://www.dailynewsounds.com/wp-content/uploads/2016/01/tinashe-5535799554a1b-3.jpg&g=R\%26amp\%3BB&p=8\%20hours\%20ago&s=+Energy&a=Tinashe+Ft.+Juicy+J+ [L]
And this works to change my urls but we have our nasty urls directing to the pages.
http://dl.example.com/l/index.php?mp3=oo8aey7qw3n9&img=http://www.example.com/wp-content/uploads/2016/01/stuey-rock-ft-sy-ari-da-kid-569e83c69cf5e.jpeg&g=R%26amp%3BB&p=10%20hours%20ago&s=+Me+And+You&a=Stuey+Rock+Ft.+Sy+Ari+Da+Kid+
How can I make it so that I can check my url to see if it contains index.php and if it does redirect it to the new page?
Try the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /l/index\.php\?\S*mp3=\S* [NC]
RewriteCond %{QUERY_STRING} \bmp3=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
If you're using Apache 2.4.x, then replace the L] with L,QSD].

Redirect URLs from Wordpress with ?post= or ?cat= in htaccess

So I need to redirect a mass of old Wordpress short urls that begin with question marks such as:
/?post=731
Or
/?cat=73
I need them to be instead /page
I tried
RewriteRule /?cat=73$ http://www.mydomain.com/page? [R=301,L]
Didn't work
I tried
RewriteCond %{QUERY_STRING} ^cat=73$
RewriteRule ^/$ page? [L,R=301]
Didn't work.
Each old URL will have to be manually redirected to a new url. Meaning:
/?cat=73 goes to /category-73
/?cat=2 goes to /main-category
/?post=731 goes to /this-page
These are just examples.
Each old link will have a NEW link - no one size fits all.
I have about 500 URLs like this. Anyone got any good ideas?
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=73$ [NC]
RewriteRule ^ /category-73? [L,R=301]
RewriteCond %{QUERY_STRING} ^cat=2$ [NC]
RewriteRule ^ /main-category? [L,R=301]

redirect old urls without index.php and parameters

I was able to get my old URL's with index.php to redirect to my new clean URL's
eg: example.com/index.php?gender=m&height=70&weight=150
to
example.com/men/70-inches/150-lbs/
However I've been trying to hack my way getting the other URL's without the index.php to also redirect to the clean url
eg: example.com/?gender=m&height=70&weight=150
to
example.com/men/70-inches/150-lbs/
my current redirection for URL's with index.php that works looks like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^index\.php$ /men/%1-inches/%2-lbs/? [L,R=301]
I've tried this, for URL's without index.php but it doesn't work:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^/$ /men/%1-inches/%2-lbs/? [L,R=301]
Problem is google has indexed a bunch of my page with the example.com/?gender=m&height=70&weight=150 and I want to have them all pointing to the one clean URL.
You have the correct idea, but your rewrite rule's pattern won't work: ^/$. The URI's sent through rules in an htaccess file has the leading slash removed, so there's no / URI ever, it'll just be blank:
# no slash---v
RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]
So the whole thing should be:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gender=m&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^$ /men/%1-inches/%2-lbs/? [L,R=301]

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

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]

Resources