I have a url "www.example.com/abc/abc.php"
If I change it to "www.example.com/abc/abc.php/asdasd", some kind of infinite loop starts and the server's memory peaks to 100%.
I have heard that there's some way by .htaccess by which I can redirect any "abc.php/asdasd" to "abc.php" only. Please help how, as I am not able to understand it from other examples mentioned on net.
NOTE : I dont want this "/" to be removed if it is put at the end of directories though.
This would redirect one URL to another:
Redirect 301 /abc.php/adssd http://www.example.com/abc.php
However, this will only handle this one example. You should post your full .htaccess file so we can see what's really going on
I was able to achieve what I wanted by the following htaccess code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php/(.*) $1.php [F]
</IfModule>
This may not be perfect, but gets the job done. Now anything after the text 'abc.php' results in a page-not-found.
Works for cases like :
www.website.com/abc.php/
www.website.com/abc.php/asd
www.website.com/abc.phpasd
www.website.com/abc.php?
etc.
Related
fellow programmers,
It's my first time using htaccess for rewrite purposes and i can't figure out a solution for my problem.
I have created a simple rewrite rule to redirect my users to a cleaner url using php GET variables.
Here's the code
RewriteEngine on
RewriteRule ^page/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) index.php?dir=$1&admin=$2 [NC,L]
ErrorDocument 404 /page/404
For now this example doesn't work unless both parameters are set. If i'd like to visit
mysite.com/page/dashboard
then it redirects me to 404 but if add
mysite.com/page/dashboard/random
Then i get my dashboard
I need it to work both ways, if only dir is set and if both are set
Also can i remove that /page/ directory without it messing up my styles, scripts etc? Whenever i remove /page/ and leave it just mysite.com/whatever then my styles and scripts stop working because i guess it's expecting those parameters to be met.
I know it might sound a bit confusing but hopefully someone could help me accomplish this.
So i fixed this issue, seems like the issue was because i didn't check if the url had a slash at the end or not, so i added this.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ index.php?dir=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?dir=$1&admin=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?dir=$1&admin=$2
ErrorDocument 404 /page/404
Notice the duplicate entries.
I also added a $ sign at the end so that apache would not except any more redirects or parameters.
so I have this htaccess entry:
RedirectMatch /([a-zA-Z0-9]+).php /dirA/$1.php
The goal is that any .php that is on the root directory should be redirected to /dirA/*.php
eg. suppose I make the request
domain.com/something.php
it should instead redirect to
domain.com/dirA/something.php
However when I put that entry in my .htaccess file and then I go to domain.com/something.php
it instead returns
"The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for
this address in a way that will never complete."
Any idea how I can modify my htaccess to accomplish what I want to do?
Updated Question
Also is there a way to make it so that it only redirects if the file doesn't exist in the root directory...hence if x.php exists in root, serve that x.php otherwise redirect to dirA/x.php
mod_rewrite is an overkill for this, you were on the right track with RedirectMatch. Your rule, however, is a bit faulty: the regex /([a-zA-Z0-9]+).php matches all string that contain the specified substring, so it matches "/foo/bar/baz.php", but also "dirA/foo/bar.php" (and even "/foo/bar.php/baz.php"I. Your redirection ended up in an endless loop because there was no stop condition: /dirA/foo.php was redirected to /dirA/foo.php.
You can remedy the situation by using anchors in the regex:
RedirectMatch ^/([a-zA-Z0-9]+).php$ /dirA/$1.php
As for your second question: that might indeed call for mod_rewrite. Something along these lines should do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+).php$ /dirA/$1.php [R=301]
I haven't tested it, but this should get you started. Make sure to check out the manual for details, or just search around on SO, there are tons of questions about this.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+).php /dirA/$1.php [R=301,L]
This one should be just fine
RewriteRule ^(.*)$ /subdir/$1
I am trying to redirect from one url to another using a htaccess file. I have got them all working except from one which causes an infinite redirect loop. The url I wish to redirect from is:
http://website.co.uk/author/ and i want to redirect to http://website.co.uk/author/authorname
Any ideas would be helpful
Sounds a lot like your .htaccess redirect rules are doing pattern matching on your domain name, so that when you redirect to /jamescrawford it matches against www.pragencyone.co.uk/author/ and tries to redirect again.
If you're trying to catch everything that matches http://www.pragencyone.co.uk/author/.* then you'll need to exclude http://www.pragencyone.co.uk/author/jamescrawford (and potentially any assets it uses like images, if they're in the same directory) from being matched by the pattern you're using.
You might try posting the actual rules that you're using to do the redirect, though obviously be sure not to post anything any info from your .htaccess that would compromise your server's security.
try this in your htaccess file
RewriteEngine On
DirectoryIndex index.php
AddDefaultCharset On
Options +FollowSymLinks -Indexes
RewriteRule ^author /author/jamescrawford [L]
RewriteRule ^author/ /author/jamescrawford [L]
i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.
I have a multilanguage site and I'm trying to rewrite the URL's with a fake directory something like this:
http://localhost/theSite/page.php?id=param&cat=param?lang=en,fr,es
to http://localhost/theSite/(en|fr|es)/page/param/param
.htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(fr|en|en)/(.*) $2.php?id=$1&cat=$2&lang=$3 [NL,QSA]
This resolves as a 404 error.
Any help will be apreciate.
RewriteRule ^(en|fr|es)/(.*?)/(.*?)/(.*) $2.php?id=$3&cat=$4&lang=$1 [NC,QSA]
I suppose you meant NC (no case), not NL. You referred to capture groups that didn't exist and repeated $2.
You're second capture will capture everything until the end of the URL. So it is possible you are doubling up on the extension or the wrong directory.
Although it shouldn't affect the redirect, you don't have a third capture, so where is $3?
Look at your headers and see where it is really redirecting to and comment back.