Rewrite Old dynamic URL to new Dynamic URL - php

My website was using the below URL format
localhost/loc-New-Delhi-India (loc - was common in all URLs and the text after it used to change)
Now, I have changed it to
localhost/New-Delhi-India/location (Removed the loc and placed it at the end of URL as 'location'). For this, I'm using the below rewrite rule
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
cityurl grabs the string after 'localhost' and searches the db.
Now since the website is heavily indexed by Google and shared a lot on social media, I want to redirect those urls to the new URLs. I tried the below code, but this just doesn't work.
RewriteEngine on
RewriteRule "loc-(.*)" "^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1" [R]
The new URLs are created successfully, but the old one's fail to redirect to the new one. I'm sure something's wrong but cannot find it. I researched the old questions but they are a bit different. Here, I'm not including any subdirectory after localhost and adding a directory 'localhost' after the city name.
Any help will be appreciated a lott :)

The following would probably work:
RewriteRule loc-(.*) $1/location [R=301,QSA]
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
The first rule will trigger a 301 redirect which will also tell any search engines that your resource has been permanently moved and that would probably make them change their indexes to reflect this. Once you've redirected the 2nd rule should trigger as normal.

Related

htaccess URL Rewriting issue redirecting to localhost/c:/ on XAMPP

I'm building a new version of a PHP app that has over 180k news items stored in the Database. Many contain hardcoded links in the news.body to the old file structure, which points to something like this people/7768-denis-grabe/ while the new file structure should be single-player.php?id=7768.
I'm really bad with REGEX and have very little understanding of how to work with .htaccess but I've been trying for two days to make a URL Rewrite rule which I think it's almost there but not quite yet.
In my .htaccess I have:
RewriteEngine On
RewriteRule ^people/([0-9]+)/?$ single-player.php?id=$1 [L]
It's not just rewriting the end of the URL, it's redirecting to the actual file, appending the http://localhost:c/xampp/... which gets me a 403 error forbidden page:
http://localhost/C:/xampp/htdocs/myapp/single-player.php?id=7768
All I need is to get the ID out of that first URL and rewrite it so it would redirect to the same address and just insert the ID as a GET request.
I'm working with XAMPP, mod_rewrite and AllowOverride All are already on
Thank you!
See if maybe this works?
RewriteRule ^people/([0-9]+).*$ single-player.php?id=$1 [L,NC]

Apache mod_rewrite rule for redirecting an url from one with directorys to one with a hashtag

Im trying to write a redirect rule with mod_rewrite, but somehow it never functions, so i must be doing something wrong :(
The below is the rewrite rule as i've defined it in an .htaccess file, it should be noted that there's another block above this one, that comes from WordPress, i tried combining that one and the below into 1 rewrite rule to see if that was maybe the issue, but that also didn't fix things. Anyway, this is what i have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} =facebookexternalhit\/[0-9]+(\.[0-9]+)*
RewriteRule ^/hash/([A-Za-z0-9-]+)/?$ \#$1 [R]
</IfModule>
The RewriteCond part checks if the request comes via Facebook (kept out of the equation so far during my testing of the RewriteRule) i'm not sure if this actually works, but i came across it in multiple StackOverflow questions, so i'm assuming that's fine (honestly it doesn't really matter, and i may omit that condition all together, just posted here in-case it turns out to be invalid)
The RewriteRule part should redirect all url's containing /hash/ too url's containing #, so what this means is that http://www.example.com/hash/home should redirect the browser too http://www.example.com#home
I've tried a load of different iterations of the above, but whatever i try, it simply doesn't appear to function, hence this question, does anybody see anything wrong with the code? would it be an issue that there are multiple blocks defined in 1 .htaccess file?
Finally, is this even a valid solution? the actual underlying problem is this:
I have a site that uses hash-tags to navigate to certain pages, in the site there are some sharing buttons that share the current url a user is looking at, Twitter LinkedIn and Google+ all handle the shared url's (containing a hash-tag) without issue, but Facebook strips the hash-tag from the url in the sharer (if send it along as # hash-tag, if i try send it along as url encoded %23 hashtag Facebook reports a 404 on the url), so i need to make a workaround for Facebook where i can share a url without a hash-tag with Facebook, and then redirect those url's to something actually understood by the website when a visitor or the Facebook sharing api 'comes knocking'.
Any help highly appreciated!

Rewrite to add a query string in wordpress

I know this can be done using a wordpress function but I'd like to add this rule in my htaccess file.
Basically I want to add a parameter to the URL and then let wordpress go about its business..
I have this
RewriteRule ^events/all/ /events/?all [L]
But I'm getting a 404 when I try and go to /events/all/
Thank you for any advice
Joe
Make sure you add that rule before any wordpress rules. Wordpress routes everything to index.php and if the routing happens before any specific rewrites that you have, those rewrites will never get applied.
Additionally, wordpress uses the request information to handle routing, so it may see /events/all/ as the $_SERVER['REQUEST_URI'] var and get confused. So wordpress may not be able to go about its business, depends on how you've setup the wordpress end of things.
If for whatever reason it's still not playing nice with wordpress, you could try adding the P option to internally proxy the request:
RewriteRule ^events/all/ /events/?all [L,P]

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Htaccess rewrite rule .aspx to .php

Background: I have a website that has been built with ASP.NET 2.0 and is on Windows hosting. I now have to rewrite my site in PHP and move it into Linux hosting. I have a lot of incoming links to my site from around the web that point directly into the old .aspx-pages. The site itself is very simple, one dynamic page and five static ones.
I saved the static .aspx pages as .php-pages and rewrote the dynamic page in PHP. The dynamic page is called City.aspx and I have written it in PHP and it is now called City.php.
On my old Windows hosting, I used ASP.NET's URL mapping for friendly URL. For example, incoming URL request for Laajakaista/Ypaja.aspx was mapped into City.aspx?CityID=981.
My goal:
To redirect all human visitors and search engines looking for the old .aspx pages into the new .php pages.
I am thinking that the easiest way to redirect visitors into new pages will be by making a redirect, where all requests for .aspx-files will be redirected into .php filetypes.
So, if someone asks for MYSITE/City.aspx?CityID=5, they will be taken into MYSITE/City.php?CityID=5 instead.
However, I am having a lot of trouble getting this to work.
So far this is what I have found:
rewriterule ^([.]+)\.aspx$ http://www.example.com/$1.php [R=301,L]
However, I think this can not handle the parameters after the filetype and I am also not quite sure what to put on front.
To make things a bit more complicated, at my previous site I used friendly URL's so that I had a huge mapping file with mappings like this:
<add url="~/Laajakaista/Ypaja.aspx" mappedUrl="~/City.aspx?CityID=981" />
<add url="~/Laajakaista/Aetsa.aspx" mappedUrl="~/City.aspx?CityID=988" />
<add url="~/Laajakaista/Ahtari.aspx" mappedUrl="~/City.aspx?CityID=989" />
<add url="~/Laajakaista/Aanekoski.aspx" mappedUrl="~/City.aspx?CityID=992" />
I tried to make a simple redirect like this:
Redirect 301 Laajakaista/Aanekoski.aspx City.php?CityID=992
but was not able to get it to work. I ended up with an internal server error and a 50k .htaccess-file...
Any help is greatly appreciated.
Do you really want to do the cityname / ID translation in the htaccess file?
The usual way would be to match Laajakaista/*.aspx and to pass the place name as a parameter to the PHP script (which would then do the translating into an ID). Wouldn't that be a better idea?
If that is an option, you would do something like this:
rewriterule ^Laajakaista/([.]+)\.aspx$ city.php?cityname=$1 [QSA]
rewriterule ^([.]+)\.aspx$ $1.php?%{QUERY_STRING} [R=301,QSA]
You can do that with or without a 301 - a 301 will change the URL visible in the browser.
Try adding QSA to the flags, so that it's [R=301,L,QSA]. This stands for "Query String Append" and will append all of query string from the old URL to the new URL. This should solve your issue of the parameters after the filename.
RewriteRule ^(.*)\.aspx$ http://www.example.com/$1.php [R=301,L,QSA]
As for the other mappings, you may want to re-think your method. Having a mapping for each city seems messy. You should probably pass the city name as a parameter to the city.php file and have that convert from name to ID. Then it's only one RewriteRule entry in the .htaccess file, rather than a redirect for each city. For example,
RewriteRule ^Laajakaista/(.*)\.aspx$ city.php?cityname=$1 [L]

Categories