Mod_rewrite url vice versa redirect - php

I can't understand and can't find the needed clear information about it.
Is it even possible to rewrite with mod_rewrite localhost/mysite/index.php?a=search url to localhost/mysite/search?
I have tried with wikipedia http://en.wikipedia.org/w/index.php?title=Dog, then it immediately redirects to https://en.wikipedia.org/wiki/Dog. I want similar thing.
I've tried this code on my localhost site .htaccess file:
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L]
it worked when I tried to type in browser something like localhost/mysite/search but it is only one part of what I want. Please answer, I'm absolutely exhausted.

You need an additional redirect rule for redirecting to pretty URL. Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?a=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L,QSA]

Related

How can i redirect /?lang=en to /en?

I'm having trouble redirecting a multilingual website one page. My redirection to htaccess doesn't operate properly. I need assistance with how this can be done. In my htaccess, when I add these two lines
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^ /en? [R=301,L]
redirect working, but mysite.com/en show 404 not found. Someone help me with this?
Your redirect rule as shown in question is working fine. Bot you also need a rewrite rule to handle /en:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
If your browsers ends up at mysite.com/en, your redirection does work, so your question is not how to redirect, but why there's no content at /en on your site. You need to investigate that.

.htaccess - rewrite one uri, but all redirected to HTTPS

I am really struggling to get .htaccess correct. I basically want to convert
product.php?sku=M1234 for example to display as M1234.php and to be able to type ../M1234.php to take to perform that operation. I also want all pages on the website to be HTTPS.
I have tried this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,N]
RewriteRule ^([^/]*)\.html$ /products/product.php?product_id=$1 [L]
With no success. Any advice would be most appreciated.
Many thanks
Use:
RewriteRule ^([A-Za-z0-9-]+)/?$ /products/product.php?sku=$1 [NC,L] # Handle product requests
This will use website.com/test to request website.com/products/product.php?sku=test

mod_rewrite: rewrite existing files to friendly links

I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]

.htaccess Remove PHP get data from URL

Well I'm making profile pages so right now it looks like this
http://example.com/random/?user=Robert
What I want to do is remove ?user= from the URL so the page appears as
http://example.com/random/Robert
Iv'e searched and I can't find anything working for me.
Thanks!
Based on http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html this should do the trick:
RewriteCond %{QUERY_STRING} ^user=(.*)$ [NC]
RewriteRule ^random$ random/$1 [NC,L,R=301]
The first step is to make all of your links in this form http://example.com/random/Robert, then in the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^random/(.+)$ /random/?user=$1 [L]
But to handle 301 redirects to your old URLs, you can include this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /random/\?user=([^\ ]+)
RewriteRule ^random/$ /random/%1 [R=301,L]

.htaccess problem, need to add string to certain URL's

In my infinity stupidity I changed the permalinks on my wordpress blog a little while back and have now changed them again. The problem that I have now caused is that I have a few hundred URL's out there which no longer work.
For example this URL looks like the ones that used to work
http://www.lazygamer.net/the-evopoints-co-za-downloads-of-the-week-1305/
But you'll see that just gives you a 404 not found page now because my site expects the first subdirectory to be a category such as
http://www.lazygamer.net/xbox-360/the-evopoints-co-za-downloads-of-the-week-1305/
So now I want to put a htaccess rule in that checks to see if a category exists and if it doesn't then just add in something random to make the url resolve.
I'm pretty sure I can do this with a regular expression of sorts but I can't figure it out.
[Update] My current .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
RewriteRule ^index\.php$ - [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.net$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
Read up mod_rewrite to understand how to use these for other problems!
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
That should do it.
Beware, this will redirect anything with one directory path in the URL to /category/{original_url_path}

Categories