Htaccess rewrite rule .aspx to .php - 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]

Related

Rewrite Old dynamic URL to new Dynamic URL

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.

.htaccess url rewrites for white label sites

I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]

PHP Site Generating Strange Pages

I have a dynamic PHP based site and I've recently noticed its generating a lot of weird pages like this:
http://www.festivalsnap.com/festival/3151748-16th+Annual+Magnolia+Fest+/hotels/3151748-16th+Annual+Magnolia+Fest+/ticket/hotels
The site architecture should be like this www.mysite.com/festival/ and then there are 4 possible child pages for each event... /lineup /tickets /hotels /news
As you can see from the URL it just keeps creating more and more unwanted child pages. When I run a sitemap generator it will just keep going forever and creating more of these pointless pages.
It shouldn't go any deeper than the /hotels page but for some reason its just adding more and more child pages using any combination of the above pages.
I'm no good with PHP and my developer isnt being very helpful. Anyone know what could be causing this?
Edit:
The main event page comes from a file called festival.php and then there are 4 child pages under that - lineup.php tickets.php hotel.php and news.php that get variables from the event page (event title, dates, location, etc) and use it to search for tickets, hotels, etc.
I have noticed that I can tack on basically anything to the URL and it will add it in as part of the page title/event title. It looks like there is something weird going on with .htaccess
Here is the .htaccess code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.festivalsnap.com$ [NC]
RewriteRule ^(.*)$ http://www.festivalsnap.com/$1 [R=301,L]
RewriteRule festival/(.*)-(.*)/lineup$ lineup.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/news$ news.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/tickets$ ticket.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels$ hotel.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels/(.*)$ hotel.php?eveid=$1&festival=$2&hsort=$3
RewriteRule festival/(.*)-(.*)$ event_page.php?eveid=$1&festival=$2
RewriteRule artists/(.*)-(.*)$ artists.php?artid=$1&artname=$2
This is partly something to do with your generator, and partly to do with .htaccess. The .* operator is extremely aggressive, so your .htaccess file says pretty much anything containing festival/ with a hyphen somewhere later in the URL is a valid URL.
But that doesn't explain why your generator is "finding" all of those pages; there must be some bad links being created somewhere, either internally in the generator or in links on pages on your site.
Can you post some code?
EDIT: The .htaccess code should be much narrower - try replacing each of the occurrences of (.*) with ([^/]*).
As for the PHP, it's impossible to say exactly what is going on, but it sounds like the generator is finding those links on your site somewhere and following them, in which case the sitemap generator is working correctly, but your content has problems. Check your logs, find one of the incorrect URLs, and see what page referred the user there. That will tell you where to look for the bad code.

htaccess for dynamic web pages

I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL
For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm
And so on for them, one more thing do i need to add this for every page? the data is being update on daily basis so is there any easy way to write URL automatically?
Any help highly appreciated.
I don't really think what you are trying to accomplish has anything to do with mysql. What you are looking for is called URL rewriting. There are countless number of articles out there that could show you the direction to follow. I am not very sure which web server you are using right not. I presume it is Apache. Here is Apache module_rewrite guide.
Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely.
So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page.
So in htaccess, something like...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]
Then in controller.php you parse the url and load the correct page.
A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this:
http://www.mywebsite.com/flights/10/Africa/Entebbe.htm
This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests).
So in htaccess...
RewriteRule
^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
flights-result.php?id=$1&country=$2&place=$3 [L]
Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not.
Remember to change all the links your app outputs to the new style as well.
You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :)

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories