Error on rewriting URL with htaccess - .htaccess

I want to rewrite URL with help of ".htaccess" file, but having some problem.
http://domian.in/code.php?code=hby5
to
http://domian.in/hby5
I used this below htaccess code but having "500 Internal Server Error".
RewriteEngine On
RewriteRule ^([^/]*)$ /code.php?code=$1 [L]
Any help will be appreciable.

Your rule is causing an infinite internal redirection ( /code.php => /code.php.... ) because ([^/]*) also matches your rewrite destination /code.php and rewrites it to itself , to fix this ,you need to exclude the target /code.php from the rule
RewriteEngine On
RewriteRule ^((?!code\.php)[^/]*)$ /code.php?code=$1 [L]

Related

How to get /folder/subfolder to display as /subfolder/ using htaccess

I'm trying to do the following...
Original path that needs to be rewritten, renamed, or redirected:
http://www.example.com/_plugin/notifications/
to:
http://www.example.com/notifications/
via root htaccess file...Any help would be greatly appreciated!
=================================================================
This does not work:
RewriteEngine On
RewriteRule ^/_plugin/notifications$ /notifications [L]
This does not work either:
RewriteEngine On
RedirectMatch ^/_plugin/notifications$ /notifications/
This does not work either:
RewriteEngine On
RewriteRule ^notifications/(.*)$ /notifications$1 [R=301,NC,L]
=============================
EDIT
With the help of #Starkeen - I have a few follow up questions to this.
Is there a way to shorten the .htaccess file up though if lets say I
have multiple subfolders within the folder instead of writing that 1
condition and the 2 rules for each subfolder?
How would I allow subfolders of the subfolder to display? Currently it is throwing a 404 at me... :(
============
Still need help with follow-up question #1, but I believe I got question #2.
SOLUTION (I believe) to follow-up question #2:
RewriteCond %{THE_REQUEST} /_plugin/notifications/ [NC]
RewriteRule ^_plugin/notifications/(.*)$ /notifications/$1 [L,R]
RewriteRule ^notifications/(.*)$ /_plugin/notifications/$1 [L]
None of the rules you have tried are correct.
To redirect /folder/subfolder to /root/subfolder you need a permanent 301 Redirect rule something like the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} /folder/subfolder/ [NC]
RewriteRule ^folder/subfolder/$ /subfolder/ [L,R]
The above will redirect http://example.com/folder/subfolder/ to http://example.com/subfolder .
You will get a 404 error if the /subfolder/ doesn't exist in the root dir. To avoid the 404 error you can rewrite the /subfolder/ uri back to its original location /folder/subfolder/ using an internal Rewrite just bellow the first one
:
RewriteRule ^subfolder/?$ /folder/subfolder/ [L]

URL rewrite for / causing 500 internal server error

I have a rewite rule that should redirect the index page (/) to a different page so I can deal with all of my content in there. However adding this second rule caused a 500 internal server error and I am not sure why.
This is in my .htaccess file
Its the 3rd line:
RewriteEngine on
RewriteRule ^search/(.*)/?$ search.php?postcode=$1
RewriteRule ^(.*)$ /turnstile.php?goto=home [L]
You have to exclude the destination you are rewriting to.
RewriteRule ^((?!turnstile.php).*)$ /turnstile.php?goto=home [L]

Unable to get .htaccess RewriteRule to work

I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

Why my htaccess rule is not working unless it's directed to a PHP file

My htaccess file is under "myappname" folder.
I'm trying to redirect this path;
myappname/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
To that path
myappname/views/default/tpl/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
and this is my HTACCESS rule
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/$2.$3 [L,NC]
but it's giving me HTTP 500 unless I redirect it to a PHP file like
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/index.php?a=$2.$3 [L,NC]
What is wrong with my rule? I'm very new to htaccess and there is a very big potential to I'm missing something small.
This is because your target matches the regex:
views/default/tpl/foo/bar.png
matches the regex:
^(.*)/(.*)\.(css|js|gif|jpg|png)$
So the rules just keep looping. You need to add a condition:
RewriteCond %{REQUEST_URI} !^/views/default/tpl
right above your RewriteRule.

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources