Rewrite old mediawiki URL pattern to short URL format - apache

We have been running a mediawiki installation for some years now. During an upgrade to a newer version, we decided to switch to the short url pattern wikipedia uses. This is working fine.
So our config file now looks like this
# Enable the rewrite engine
RewriteEngine On
# Short url for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [NC]
As specified in the documentation for mediawiki.
But we have some old URLs floating around other places, which we would like to support still.
The old URL would look like this:
wiki.domain.com/wiki/Index.php/articlename
Now this URL looks like this:
wiki.domain.com/wiki/articlename
But using the old URL, now just gives a page not found.
We have tried using an online htaccess tester to find the correct RewriteRule. And we get it to work there. But not when using it in on our apache server config.
Tried with this rule, right after RewriteEngine On:
RewriteRule ^(.*?)index\.php(/|$) /$1 [R=301,NC,NE]
Which is supposed to redirect the user to a new url. But we still get the page not found error, and the URL still looks the same.
Is our new rewrite rule wrong, or might there be something else wrong in the apache config. Will provide the config file if need be.

Ended up with this rule:
RewriteRule ^(.*?)index\.php\/(.*) $1$2 [L,NC,R=301]
Select the parts before and after index.php and store them using (). Using $1 and 2 to create a new URL string. I'm sure there are better solutions, but this works too.

Related

Change URL in browser without redirection with htaccess

I've looked everywhere to find the proper solution/method but I can't seem to find anything that works for me.
I even asked friends and they helped but none prevailed.
What i'm trying to do is, changing the URL displayed in the browser but only that. (No rediraction, page re-loading).
I want to do this to make my UCP just cleaner looking when going through certain pages/files.
What am I trying to achieve?
Heres an example on a profile, the URL would be:
mysite.com/ucp/profile.php?player=Heartfire
However, I want it to look like
mysite.com/ucp/profile/heartfire
Or something else! I just want to get rid of the parameters AFTER the .PHP
I've tried various examples found with google and this website but none seems to work, could somebody please guide me along the way to achieve the result.
what have I tried so far?
Here are a few examples of what I tried before:
RewriteRule ^profile/([0-9]+)/?$ /ucp/profile.php?player=$1
RewriteRule profile.php?player=$1 profile.php [NC,L]
RewriteRule ^profile$ profile.php?player=$1
So what am I doing wrong that it isn't working?
Put the following in .htaccess file inside website's root directory:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /ucp/profile\.php?([^=]+)=(\S+) [NC]
RewriteRule ^ucp/profile\.php$ /ucp/%1/%2? [R=301,L,NC]
# Now, deal with internal rewrites (which will not cause redirection):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ucp/([^/]+)/([^/]+)/?$ /ucp/profile.php?$1=$2 [NC,L]
You can use internal redirects what will not change your url but map your request as your wanted.
What you want is impossible because:
Htaccess and rewrite is at server side. The request arrived to the server, need to rewrite at serverside and you need to change it in the clients url bar.
To achieve this the server should send a redirect with the url what you expected. This ia why redirect is mandatory. Server can't rewrite clients urls, just can send a redirect response.
Internal redirect can simulate you something like the request was what you expected but it is transparent at for the clients.
Btw, permanent redirect is the right solution here to notify the user and give the chance to let them know the resource has been changed and update the bookmark / api / whatever.

RewriteRule Redirects paths not working

I moved my website to a new server with a new CMS so I had to make a lot of 301 Redirects. 'Normal' 301 redirects didn't recognize the url path of my old urls so I tried to make RewriteRules, this is what it looks like now:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^Category http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
The first RewriteRule works, but as soon as there is a second path in the old url (the second example) the redirect will point to the main cateagy and not the subcategory. So it's basically ignoring the url paths...
Try to invert your rules, or to add a $ at the end of the first one :
RewriteRule ^Category$ http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
Explanation : Category/Subcategory is also matching the first rule, and as you have use a L flag in the first one, Apache will just use this first rule and don't bother to look further.
For general purpose solution, quoting from apache rewrite guide:
Move Homedirs to Different Webserver Description:
Many webmasters have asked for a solution to the following situation:
They wanted to redirect just all homedirs on a webserver to another webserver. They usually need such things when establishing a newer webserver which will replace the old one over time.
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just
redirect all /~user/anypath URLs to http://example.com/~user/anypath.
RewriteEngine on
RewriteRule ^/~(.+) http://example.com/~$1 [R,L]
In your case URL structure has changed so ôkio's suggestion would work.

URL: transform "/news.php?id=1" to "/news/1/"

I'm trying to make some more cleaner URL's for my website. I have allready found lots of questions regarding the removal of file extensions in URL's and managed to get that working. I want to take it a step further though by transforming an URL like http://www.site.com/news.php?id=1 into http://www.site.com/news/1/. I couldn't find an answer to this specific question so I'm asking it here. How could I achieve URL's like that?
Rewrite rules I allready have in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
There are plenty of questions out there that answer your question. There are two things you have to do:
Redirect the ugly url to the fancy url, so that the user sees the fancy url
Internally rewrite the fancy url to the ugly url, so that the server can actually execute it.
You can find the documentation for mod_rewrite here. This solution uses the [END] flag which is only available from Apache 2.3.9 and up. You can find the documentation here. The trailing ? in the first rule discards the query string.
#Redirect ugly url to fancy url
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^news\.php$ news/%1/? [R]
#Internally rewrite fancy url to a usable url
RewriteRule ^news/([^/]*)/?$ news.php?id=$1 [END]

htaccess mod_rewrite giving me 404

SO I have this in my .htaccess file
RewriteEngine On
RewriteBase /core/
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^page page/%1/? [L]
my url is
http://localhost/core/page.php?page=8
with the rules applied I'm getting..
Not Found
The requested URL /core/page/8/ was not found on this server.
This is running on wampserver 2.2
the file structure looks like
c:/wamp/www/core
the .htaccess is inside the /core/ directory.
What is it that I'm missing.. i've checked my apache.conf file and it looks fine.
I think you got it the wrong way around. When logically thinking of rewriting you don't rewrite original URL to new URL (for example page.php?page=8 to page/8/) you actually rewrite page/8/ to page.php?page=8. You tell the server how it should interpret the unfamiliar URL.
So if I understood correctly what you want to achieve is:
User visits localhost/core/page/8/
User is served (under the hood) localhost/core/page.php?page=8
I believe the following RewriteRule will do the trick (The query string condition is not necessary):
RewriteRule ^page/(\d+)/$ page.php?page=$1 [L]

mod_rewrite redirect specific .asp pages back to original server

I need to redirect traffic for a section of a redeveloped site back to the original site with apache mod_rewrite rules. I need to redirect all requests starting with http://www.example.com/page.asp back to the original site http://www.original.com/page.asp with the query string or anything following page.asp intact.
This seems simple enough, however I have had no luck with mod_rewrite generators or documentation on the web. My latest stab at the problem looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.example.com$ [NC]
RewriteRule ^page\.asp(.*)$ http://www.original.com/page\.asp$1 [R=301,NC]
I appreciate any insight into correcting this mod_rewrite rule. Other redirects are working fine.
I was able to get the rewrite to function properly using:
RewriteRule ^/page(.+) http://www.original.com/page$1 [R=301,NC,L]
I found this apache doc helpful: Resource Moved to Another Server