htaccess, fake subdirectory to root - .htaccess

I would like this URL ->
http://www.example.com/games/Mario_Kart
to be redirected here ->
http://www.example.com/games.php?name=Mario_Kart
I'm a total beginner and i tried this :
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^games/[A-Za-z0-9_-]{0,100}$ games.php?name=$1 [L]
It worked but all the paths were broken (no css, ...)
And now, i don't know why but I have a 403 error.
I just want to know if my code is OK (I guess not)

What you want to do is :
RewriteEngine on
RewriteRule ^games/([A-Za-z0-9_-]{0,100}) /games.php?name=$1 [L]
Basically by addind () you can call it later with the $1, you were close to the result.

Related

Rewrite non-existing folder to be query without redirect

I need a way of getting example.com/159985870458322944/, or any other random 18-digit string, to be rewritten to example.com/?159985870458322944, where that string is a query key. It also needs to not change URL, so that the URL stays example.com/159985870458322944, but shows content that the index.php in the main directory (example.com/) gets thru the query.
I currently have this (not working)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/([0-9]+)$ /?$1 [NC,P]
</IfModule>
Which does not work, as expected. I tried a lot of things I found, but didnt get any of the solutions working.
There is no need to use P or proxy for this, just rewrite internally like this:
RewriteEngine On
RewriteRule ^(\d{18})/?$ /?$1 [L,QSA]

Changed part of path and need to redirect old to new

I have updated country code in the base path and I am attempting the redirect users that end up going to:
www.example.com/uk/etc1/etc2/etc..
www.example.com/gb/etc1/etc2/etc..
But also sometimes also www.example.com/uk#etc
I thought this would be fairly trivial in mod_rewrite I cant get anything to work.
The closest I feel I have come is the following rule, but nothing happens when I land on uk.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* index.php [L] #this is active already on the page
RewriteRule ^/uk(.*) /gb(.*) [R]
</IfModule>
Edit:
This seems closer but I am getting stuck in an endless loop:
RewriteRule ^uk(.*) /gb$1 [R]
Edit 2 / (3) Answer:
This seems to redirect correctly(is correct):
RewriteRule ^uk(.*)$ /gb$1 [R=302,L]
RewriteRule .* index.php [L]
But the formatting is broken on the page, looks like some loop problem.
But the formatting is incorrect, If I swap the two RewriteRules the formatting is correct but the uk => gb conversion doesn't seem to get called at all.
Answer
Be careful of Rewriteconditions, I failed to notice the significance of them and I removed them for the original index.php rewrite which meant it could not locate any media.
I failed to take notice of the rewrite conditions which applied for the original index.php
This should do the trick:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^/uk(.*) /gb$1 [R=301,L]

Strange htaccess redirect

Whats is wrong with my htaccess here..
RewriteEngine on
Options -Indexes
IndexIgnore *
RewriteRule ^page somepage.php [L]
Im try redirect http://mysite.com/somepage.php to http://mysite.com/page and this working fine, but i can also access to this url http://mysite.com/page2121 or http://mysite.com/pageasd4a4sd ... and all is redirected to same page somepage.php, what is wrong here..
Add the $ at the end of page like the following:
RewriteRule ^page$ somepage.php [L]
Assuming you mean redirect /page to /somepage.php, you need to explicitly set the end of the pattern, write now, yours is matching everything that starts with page, not starts and ends with page. Try this:
RewriteEngine on
RewriteRule ^page$ somepage.php [L]
(the $ is key)

htaccess redirect according to IP

Let's say my server looks like this:
/www/.htaccess
/www/index.php
/www/temp/index.php
And my personal IP (not the server's!) is 127.0.0.1*
(* ok, that's impossible, but for the sake of argument...)
I would like to redirect (301/302) everyone EXCEPT that IP to /temp/index.php
My current code looks like:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/temp/*$
RewriteRule \$ /temp/* [R=302,L]
However I get a 500 error.
Never mind, these htaccess file are hard to work with. I used some PHP code in my main index.php file instead.

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources