I am having problem in URL rewriting using .htaccess file, problem is as below.
When I put below code in my .htaccess file on server
RewriteRule ^/?xyz http://google.com [R=301]
and I call url MY_WEB_DOMAIN.COM/xyza in browser it redirects me to google.com ( as expected )
And if I call url MY_WEB_DOMAIN.COM/xyz/abc I get 404 error..
isn't it should redirect to google on both case?
or something else is affecting my code with I use / (slash) in my pattern?
Thanks in advance.
arkascha is right. Use this:
RewriteRule ^/?xyz.* http://www.google.com [R=301]
the /? makes that first / optional, which is sort of asinine in this case. But the .* is what you want to catch anything matching /xyz-----
if arkascha posts that as an answer you should accept it.
I tested this and it works fine. If it doesnt work for you check what lines you have above this rule in htaccess. you may have a conflict.
Rewriting was working ok.
I checked headers on server side for rewritten URL and directly called url.
I found only one change.
in direct url It was containing redirect_url and request_uri were same.. for rewriten url both were different.
(I thought oxwall might be using request_uri to process.. where as it was not as I wanted to be..
So I just made them same for rewritten url in php. by placing line
$_SERVER['REQUEST_URI']=$_SERVER['REDIRECT_URL'];
And It is done. :)
Related
Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]
How do I strip a part of the url? I do not know much about htaccess or apache.
I would like to strip www.mysite.com/page=services to www.mysite.com/services for example.
What exactly do I need to put in the .htaccess file in order to achieve this, and would that work for other pages as well?
Thanks.
I tried this for one of my sites lately and came up with this and for me it works fine,
this goes into the .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
then if you would write yoursite.com/pagename it would send yoursite.com/index.php?page=pagename to your php.
You will link to the page like so: yoursite.com/pagename
it wont change yoursite.com/index.php?page=pagename to yoursite.com/pagename in the adress bar after you send it.(if that makes sense :) )
I hope this is what you are looking for...
I think you mean your original URL to be www.mysite.com/index.php?page=services and not www.mysite.com/page=services
Also, you probably mean the opposite, you should switch www.mysite.com/services to www.mysite.com/index.php?page=service
Anyway, to change www.mysite.com/services to www.mysite.com/index.php?page=services then you need .htaccess, and the rule for that would be RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
As suggested earlier, you should read about .htaccess, regex, and rewrite rules. Best resource is the apache documentation here: http://httpd.apache.org/docs/2.2/howto/htaccess.html
I read your comment earlier that you need the opposite, I am not sure why you need that, because the whole idea of URL shortining is to make easy-to-remember URLs in addition to some security concerns that can be resolved. The URL is the first thing that is sent to load your webpage, then .htaccess changes it to some form undrstandable by PHP then PHP deals with get parameters for example.
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
the problem is that for some reason googlebot gets a bunch of error 404s when crawling a certain page but everything works fine on my end, I think...
using htaccess, I have rewritten a page with vars for seo purposes, so:
RewriteRule ^feeds/([^/]*)\.xml/?$ /rss/feeds.php?cat=$1 [QSA,NC,L]
so I get www.url.com/feeds/category.xml
instead of
www.url.com/feeds.php?cat=category
I've also ajusted all urls on the page to follow this rule and like I said everything works fine on my end... googlebot for some reason only sees www.url.com/feeds/category and does see the .xml extention.
not sure if it would work but I was hoping to put a rule in htaccess to redirect www.url.com/feeds/category to www.url.com/feeds/category.xml in the even that this happened, but was unsure of how to do so.
Maybe try
RewriteRule ^feeds/([^/]+)/?$ /feeds/$1.xml [R,NC]
Which would redirect www.url.com/feeds/category to www.url.com/feeds/category.xml, using a HTTP 302 Redirect. Remove the R flag if you don't want it do use the HTTP 302 Redirect, but that would make www.url.com/feeds/category a valid URL as far as google is concerned. Just in case: mod_rewrite.
I need to redirect such link:
www.website.com/index.phpsite=transport (it's a wrong link which was added to many catalogues of pages etc. and it's really important to redirect from this link to the right one)
to this one (obvious):
www.website.com/index.php?site=transport
All is about the missing "?" in address.
So I wanted to use .htaccess and RewriteRule, but dunno how.
Can you help me? How to write the rule that will replace wrong link with the working one?
Thanks in advance,
Konrad.
The following should work for you (although unfortunately I do not have time to fully test the code):
RewriteRule ^index\.phpsite=(.*)$ /index.php?site=$1 [R=301,L,NC]
I have used the .* wildcard on the end as I wasn't sure if you could have more URL parameters in some URLs you needed to redirect.
All this behaviour is documented in the related mod_rewrite manual pages.
If you are using Apache server, then Write a redirection rule in htaccess file.
RewriteRule URL1 URL2 [R=301,L,NC]
Since it worked, this is my answer:
RewriteRule ^index.phpsite=transport$ /index.php?site=transport [L]