Unable to get Mediawiki .htaccess Short URL to work: [closed] - .htaccess

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have mediawiki set up like this:
mysite.com/w/
I am trying to add the Short URL method like it suggested in the manual here:
https://www.mediawiki.org/wiki/Manual:Short_URL/Apache
I used the redwerks tool to generate the .htaccess rewriting rules:
http://shorturls.redwerks.org/
Although I am on VPS with root access, I find changing the Apache config and Nginx config to be too complicated and way over my head. So I decided to go with the htaccess method and I couldnt get it to work completely. This is what I added the .htaccess file in the root path:
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteRule ^/?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?w/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/w/thumb.php?f=$1&width=$2 [L,QSA,B]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?w/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/w/thumb.php?f=$1&width=$2&archived=1 [L,QSA,B]
And I Added these to the LocalSettings.php
## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs please see:
## http://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/w";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";
AND
## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
$wgEnableUploads = true;
After the above, the redirection sort of works. Meaning, if I go to mysite.com/w/index.php/Page_Title, it redirects to mysite.com/wiki/Page_Title. But the following doesnt work:
1) When I type in mysite.com/wiki, it doesnt find the page and doesnt automatically goes to Main Page like it used to do when I typed mysite.com/w/. So do I need to add a separate Rewrite rule for this?
2) I am also receiving a page not found and its creating a new article page when the url looks like: mysite.com/wiki/index.php. In this case, it is trying to create a index.php article instead of directing me to Main Page.
3) Even when the above rule is on, when I got to pages other than the main article page, such as edit a page or view history, etc.. it still shows the url as mysite.com/w/index.php?title=XYZ&action=edit. Why doesnt it convert all urls to wiki?
4) If a url has a ?title added to it, it doesnt get redirected to wiki. For example, this page mysite.com/w/index.php?title=Main Page, does not become mysite.com/wiki/Main_Page
After seeing the above, now I am thinking if its better to have my wiki installation to mysite.com/wiki/ folder instead of 'w' folder. Infact thats how I had it first but later changed it when I trying to implement the Short URL method since the MW Manual suggests against it. So I need some guidance on how to resolve this please...
These are just some of the issues I've noticed so far. So basically rewrite rule doesnt completely rewrite all the pages. It only rewriting the main article page url by removing the index.php/ from the url and changing w to wiki. But other urls types are still the same and are not rewritten. When I checked the mediawiki website, all of their urls are rewritten to /wiki/ without the index.php even when editing page, etc.. How can I get the same results?

Okay after further searching on the net, this is what I found.
Even when the above rule is on, when I got to pages other than the
main article page, such as edit a page or view history, etc.. it still
shows the url as mysite.com/w/index.php?title=XYZ&action=edit. Why
doesnt it convert all urls to wiki?
This is how its supposed to work. So this means the Main pages, Namespace pages and Special Pages will have this short url while the other pages like edit page, history, etc.. will still show w/index.php/......
If a url has a ?title added to it, it doesnt get redirected to wiki.
For example, this page mysite.com/w/index.php?title=Main Page, does
not become mysite.com/wiki/Main_Page
It only rewrites the article urls and I should never include ?title=$1 or something like it in the rewrite. Including a query will cause MediaWiki's built in handling to be overridden and will create bugs on your wiki due to the fact that Apache's query rewrites are broken. The goal here is to alias paths to /index.php and then let MediaWiki itself take care of parsing and routing the url, based on the configuration in LocalSettings.php.
So basically rewrite rule doesnt completely rewrite all the pages. It
only rewriting the main article page url by removing the index.php/
from the url and changing w to wiki. But other urls types are still
the same and are not rewritten.
I later found out that thats how it is supposed to work.
I am yet to find an answer for 1 and 2. I think I need to add a separate redirect rule for that. I have asked a separate question to find the htaccess rewrite rule for that: How to redirect Mediawiki Short URL with no title to Main Page?
I will update this topic once I have the answer for those..
UPDATE:
For no: 1, I just need to move the rewrite rule to the top before other rewrite rules and it worked after that. More info: How to redirect Mediawiki Short URL with no title to Main Page?
For no: 2: wiki/index.php will not be redirected to wiki/Main_Page since it will look at index.php as a new article. It will only redirect w/index.php to /wiki/Main_Page

Related

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

Renaming and redirecting pages fails in htaccess

I am sorry to ask this question, because the answer seemingly is so easy. However, after three hours of trial and error I am without a clue.
I have several pages on a website using parameters in the url. I would like to change that, to a more regular url. Example:
domain.com/pag.php?id=1-awesome-page should become domain.com/awesome-page
So far so good, but so far I have three problems.
1. The old page still is accessible, Google will index it as duplicated content. When I try to redirect it, I am getting infinite loop errors.
2. For whatever reason, sometimes SOME images (straight from the content) get stripped off on the newly named page. I tried playing with a base-url and renaming the images and urls, but nothing so far.
3. Also the redirect doesn't care if i'd enter id=1-awesome-page or id=2-worthless-page. It all redirects to the first one.
Among the things i've tried.
RewriteCond %{QUERY_STRING} id=1-awesome-page
RewriteRule ^pag\.php$ /awesome-page? [L,R=301]
RewriteRule ^awesome-page?$ pag\.php?id=1 [NC]
What you want to do cannot really be done with mod_rewrite, unless you want to make a rule for every page, which will probably slow your site down quite a lot. This is, because you can't summon the 1 in 1-awesome-page out of thin air, and your pag.php page doesn't seem to be able to load the page only based on it's seo name. If you need to use that number, you need to have that number somewhere in your url.
As for your questions:
The error you mention cannot be reproduced with the current iteration of your .htaccess. You likely had an infinite loop previously, and since you use R=301 to test, the browser will cache this redirect and only request the second resource afterwards when you request the first resource. You should test with [R,L] and only change to [R=301,L] when everything works as expected. Not doing so will cause weird behaviour, and behaviour you do not expect with your .htaccess.
When you have an url a and an url b, and want to redirect a to b, and want to internally rewrite b to a, you need to make sure that any given time not both rules can be matched. You can either use the %{THE_REQUEST} trick or use the END flag. Both are outlined in this answer.
If you have a problem with resources on a page not loading after making a fancy url, you likely used relative url's. This question outlines the possibilities on how to resolve this. You can either make the url's absolute or relative to the root of your site, or use <base href="/">.
The following would work for /pag.php?id=123-news-page and /news/123/news-page.
RewriteCond %{THE_REQUEST} pag\.php\?.*id=([^-]+)-([^&\s]+)
RewriteRule ^pag\.php$ /news/%1/%2? [L,R]
RewriteRule ^news/([^/]+)/([^/]+)/?$ pag.php?id=$1-$2 [L]

Redirecting page links after .htaccess SEO upgrade? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I didn't want to bloat one post here with a ton of .htaccess questions, so I'm breaking them up individually. This one has to do with using RedirectMatch to create rules for existing, indexed pages that are part of an existing website.
Specifically, my test site has been up for about a year and I am now using .htaccess to re-write clean URLs for better SEO. So when I had a container page whose content was called based on a query string in a link, like so:
http://www.website.com/departments.php?dep=contact_us
I'm working on making the URL appear smoother like:
http://www.website.com/contact-us/
The tutorial I read suggested that changing my links to the new, snazzier SEO-friendly style would lead to lots of 404 errors and broken page links until the site has been indexed again and the records brought up to date.
Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?
I ask because I have several websites with many layers of file structure and I'm thinking that I'd need a TON of rules in my .htaccess file to fix all the broken links I may be creating.
If I'm creating confusion here, please let me know and I'll clarify things.
Many thanks in advance!
Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?
You can make it happen sooner if you externally redirect the client using a 301 (permanent). This is a lot trickier than internally rewriting on the server's end because you'll cause conflicting rules. For example, if you wanted to rewrite url-a to url-b internally on the server, then redirect the browser from url-b to url-a (meaning when a client specifically requests url-b to redirect it to url-a):
# internally rewrite url-a (fake URL) to url-b (where the actual content is)
RewriteRule ^url-a$ /url-b [L]
# externally redirect url-b (actual content) to url-a (fake URL)
RewriteRule ^url-b$ /url-a [L,R=301]
Since the rewrite engine loops, these 2 rules will continue to rewrite each other and cause a 500 internal server error (the same thing happens if you replace the second rule with a RedirectMatch). To fix this, you need to create a condition so that the external redirect (second rule) only gets applied if the actual request was for "url-b". You can do this by matching against the %{THE_REQUEST} variable, which is essentially the first line of an HTTP request.
Using your example URLs, you'd have something like this:
RewriteRule ^contact-us/?$ /departments.php?dep=contact_us [L]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /departments\.php\?dep=contact_us
RewriteRule ^ /contact-us/ [L,R=301]
This means when someone like a google-bot attempts to resolve http://www.website.com/departments.php?dep=contact_us, the 2nd rule's condition will match the request (which will look something like: GET /departments.php?dep=contact_us HTTP/1.1) and it will get redirected to http://www.website.com/contact-us/. At which point, the client (google-bot) will request /contact-us/ and the first rule will get applied and internally rewrite the URI to /departments.php?dep=contact_us.

Effective Mass URL Forwarding

I'm doing the migration of a site, from MovableType to WordPress. The website contain around 3500 posts and when I uploaded the information to the new version of the site, the links have been altered.
Old site link (note that the category-name and the nodenumber are not fixed values. The nodenumber is a number that change in each post):
http://websitename.com/category-name/nodenumber/title-of-the-post
New site link: http://websitename.com/category-name/title-of-the-post
My question is the following:
What would be the easiest and effective way to configure my Htaccess file to do a redirection from the old links to the new ones? There might be a simple way than to write them all by hand.
If the structure of the URLs is always like this, and the second part is always a number, the following should work in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)/[0-9]+/(.+) /$1/$2 [L]
</IfModule>

301 Redirect to change structure

I have been researching redirects for a few days now and am still struggling, so I decided to post my first question here. For some reason, it is just not clicking for me.
I have redesigned and developed a client's WordPress site and need to update it's structure.
The site's current structure is:
www.domain.com/blog/postname/2011/12/26/
The new structure should be:
www.domain.com/blog/postname
I really thought this was going to be easy since all I am looking to do is drop the date, but have not been able to grasp the whole wildcard aspect and how to end what I am trying to match. Any help would be greatly appreciated. A simple answer is great, but an explanation would be even better.
I am assuming you already know how to change your WordPress permalink structure to drop the date.
To 301 redirect all of the old URLs to the new ones, add the following rules to your .htaccess file in the root of your websites domain, ahead of any existing rules that are there.
#if these 2 lines already exist, skip them and add the rest
RewriteEngine on
RewriteBase /
# if there is a request of the form /blog/post-name/yyyy/mm/dd/
RewriteCond %{REQUEST_URI} ^(/blog/[^/]+/)[0-9]{4}/[0-9]{2}/[0-9]{2}/$ [NC]
#redirect the request to the URL without the date
RewriteRule . %1 [L,R=301]
If you want to learn more about .htaccess/rewriting you can take a look at the following urls: Indepth htaccess, Brief Introduction to Rewriting, Apache Mod_rewrite.
Let me know if this works for you and/or you have any issues.

Resources