Manual talk:Short URL/Apache

About this board

This entire page is junk and needs deleting

11
81.174.250.136 (talkcontribs)

These are the worst instructions for anything I have ever encountered.

There are just seemingly random thoughts recorded in paragraphs. Files that are mentioned are:

/etc/apache2/apache2.conf

/etc/apache2/sites-available/default.conf (this actually 000-default.conf on my distribution)

.htaccess (many places)

LocalSettings.php

Which of these files should be edited?

What is needed are instructions like this:

  1. edit <filename> - add <this line> and <this line>
  2. enable mod_somemod by typing a2enmod <mod_somemod>
  3. restart apache2 by typing systemctl restart apache2

etc.

Ciencia Al Poder (talkcontribs)

What it needs is editing, not deleting. This is a wiki, so any edit that improves the content is welcome.

2001:44C8:4400:7766:E5CD:F711:321B:495C (talkcontribs)

@Ciencia Al Poder like the anonymous user above me (believe it or not that we are not the same person) I think that you take the seriously bad situation of this webpage and how serious it misleads people, too easy.

2001:44C8:4400:7766:E5CD:F711:321B:495C (talkcontribs)

@Jonathan3 indeed, I am not the anonymous who wrote the original post but I can understand where his frustration is coming from; you told me "it's all in here" but really, sadly it's not very much the case.

Jonathan3 (talkcontribs)

I bet if you think it through and stop asking questions you'll find the answer here :-)

2001:44C8:4512:B6D6:A16F:69E9:F0A3:8BD9 (talkcontribs)

@Jonathan3 I guess you meant my recent questions on the subject in "support desk";

I nicely disagree with that approach ; I when a page is SO undidactic, it kinds of take the spirit out from cooperating, for me at least. In face I already wrote a revised version of the opener of this "guide" and should publish it soon.

Polymath uk (talkcontribs)

I'm the anonymous user. I've remembered my user/pass.


For those running debian 10 (probably Ubuntu too) with root access and apache2 do all of this as root:


1. You need to ensure the rewrite module is loaded in apache2 by doing this:

Type:

a2enmod

then when prompted type

rewrite

then press <enter>


2. Next, edit /etc/apache2/sites-available/000-default.conf by typing

nano /etc/apache2/sites-available/000-default.conf

(or your favourite editor like vi instead)

Immediately before the closing </VirtualHost> tag insert three lines that read:


RewriteEngine On

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/index.php [L]

RewriteRule ^/*$ %{DOCUMENT_ROOT}/index.php [L]


This assumes that your mediawiki installation is in the same directory that apache2 is treating as the document root. The document root path is shown in the file you have open. If your index.php file is in that directory then this will work. This will be the case if your URLs are http://mywiki.com/index.php If your URLs look like http://mywiki.com/somefolder/index.php then you will need to modify the above lines to look like this:


RewriteEngine On

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/somefolder/index.php [L]

RewriteRule ^/*$ %{DOCUMENT_ROOT}/somefolder/index.php [L]


Save the file


3. restart apache2 by typing:


systemctl restart apache2


If it won't restart you've done something wrong which you need to troubleshoot.


4. Next edit LocalSettings.php by typing


nano /var/www/html/LocalSettings.php


(or whatever path you are using)

Look for the text $wgScriptPath (which will probably be near the top)

As above, if your installation is directly in document root, this will probably read $wgScriptPath = ""; in which case, leave it alone. If you are in a subdirectory like above, it will read something different. Leave it alone anyway.

Add this line under it:


$wgArticlePath = "/wiki/$1";


Save the file.


Done.

Jcshy (talkcontribs)

Polymath you've been a lifesaver. Following your instructions got this done in about 10 seconds. Trying to use the incoherent instructions on the article itself had got me pulling my hair out after failing to make it work for an hour.

2A02:908:2057:7C80:4012:8145:F350:A140 (talkcontribs)

I used the script on redwerks.org, running wikipedia on a low-cost shared server. I copied the Apache Config and saved it as a new .htaccess in the directory that the URL should be redirected to, and changed the rows in LocalSetting.php. All done. This was just trial and error and I am happy it worked.

83.97.188.172 (talkcontribs)

Thank you so much

the "somefolder" tip solved de problem for me

Why you do not put this in the actual article?

Polymath uk (talkcontribs)

I didn't realize I could edit the article!

Reply to "This entire page is junk and needs deleting"

CSS broken, redirect to main page not working

2
95.165.152.243 (talkcontribs)

Did everything according to the guide. Instead of redirecting to main page, accessing my domain (as well as */index.php) displays a page that contents a single "+" symbol and debug outputs if switched on, and nothing else.


Also, CSS broke after fiddling with short urls, now even if I undo everything I still have this error:

Refused to apply style from... because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.


Not asking for help, but the guide probably needs to be updated.

95.165.152.243 (talkcontribs)

Never mind, my mistake entirely. Guide is OK, thanks for writing it!

Using .htaccess to remove a range of subdirs

1
Vicarage (talkcontribs)

(No luck on main mediawiki support desk)

I want to use replace an existing website with mediawiki, but to preserve incoming links I need to remove some path names using .htaccess

So https://subbrit.org.uk/sites/fan-bay-deep-shelter should become https://subbrit.org.uk/fan-bay-deep-shelter, ditto features and a few more

I already use the shortURL logic to shorten paths

LocalSettings.php

$wgScriptExtension = ".php";
$wgArticlePath = "/$1";
$wgUsePathInfo = true;
$wgScriptPath = "";

.htaccess

RewriteEngine On 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d 
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]

But my attempts to add new rules like

RewriteRule ^sites/(.*)$ /$1 [R]

just don't take effect, it just asks if I want to create a new page Sites/fan-bay-deep-shelter. The rules do work on the server away from the wiki area. I worry that I'm just changing the URL, but mediawiki is working onm the underlying URI which is not changing

Reply to "Using .htaccess to remove a range of subdirs"

It works, yet index.php still seen on navigation bar

3
Miguel.covas (talkcontribs)

I followed the process for my local wiki, and it works. Yet, the browser displays the rewritten URL.

Local wiki on my Mac Powerbook:

Scripts located on /Library/WebServer/Documents/wiki

I want to use the name of the wiki site: wikipapers.

After reconfiguring Apache (on httpd.conf) I can write:

http://localhost/wikipapers/Laplace_transform

Apache shows the proper page ... but ... on the navigation bar I still get

http://localhost/wiki/index.php/Laplace_transform


Are there any missing steps?

Ciencia Al Poder (talkcontribs)

You're missing setting $wgArticlePath = "/wikipapers/$1";

Miguel.covas (talkcontribs)

I reply to myself: I forgot to set $wgArticlePath to

"/wikipapers/$1".


Now it works fine. It also works using Alias ...

References for the VisualEditor issue?

1
Dantman (talkcontribs)

There have been some recent changes to the recommended RewriteConfig rules implying that without them VisualEditor is broken.

Can anyone provide some reference to this issue and confirm that it's real and not just speculation added by some users?

The first change targets the VisualEditor user agent but the second asserts that it's just rest.php that needs to not be rewritten. However the RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f line is already supposed to be enough to stop Apache from rewriting rest.php (otherwise it would be rewriting index.php, thumb.php, etc). So this recommendation should be redundant.

I would like to know because if it's a real change we need to make to the recommended Short URL config, then I'd like to update the ShortURL Builder tool. And if it's not actually necessary for VisualEditor to work, then the modification to the page should be reverted.

Reply to "References for the VisualEditor issue?"

Missing information on some setups

2
Rdrg109 (talkcontribs)

I'm currently running MediaWiki in my personal computer for experimentation purposes (i.e. not planning to use it in production). I'm using Apache in Arch Linux and was able to get the short URL working.

I spent more than 4 hours trying out the solutions mentioned in this documentation. Namely, I created the file .htaccess in multiple locations, edited the options AllowOverride and Options as specified in this page, but none of these ways worked. I decided to give up on Apache and use nginx instead, since there's also documentation on how to get the Short URL for nginx.

However, I then found Short URL/Page title -- Windows & Apache without 403 on Special Pages and was able to find the solution that worked for me. My solution was to add the Rewrite rules under <Directory "/srv/http"> in the file /etc/httpd/conf/httpd.conf. As a complete beginner user of Apache, I'd like to know whether this is recommended or not so that I can add it in this manual since it is not mentioned.

I'll describe what worked for me with the hope that other users find a solution faster than me and don't have to spend as much hours as I spent doing trial and error.

1. First, I wanted my links not to have mediawiki in the URL. That is, the default configuration that is bundled in Arch Linux makes the URL of the articles have the following format.

http://localhost/mediawiki/index.php/Main_Page

but I wanted the pages to exist in

http://localhost/w/index.php/Main_Page

To do this, I changed the first line of /etc/webapps/mediawiki/httpd-mediawiki.conf from Alias /mediawiki "/usr/share/webapps/mediawiki" to Alias /w "/usr/share/webapps/mediawiki"

2. Uncomment the line #LoadModule rewrite_module modules/mod_rewrite.so in the file /etc/httpd/conf/httpd.conf

3. Add the RewriteEngine and RewriteRule instructions under the section <Directory "/srv/http"> in the file /etc/httpd/conf/httpd.conf (see code block below)

(omitted lines)

DocumentRoot "/srv/http"
<Directory "/srv/http">
    RewriteEngine On
    RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

(omitted lines)

4. I then opened /usr/share/webapps/mediawiki/LocalSettings.php to make sure that the variables had the following values. By default, the variable $wgScriptPath already had that value, I just needed to add the line for $wgArticlePath below it.

$wgScriptPath = "/w";
$wgArticlePath = "/wiki/$1";

5. Finally, I restarted the service.

$ systemctl restart httpd.service

At this point, I was able to visit the Main Page using the following URLs.

http://localhost/w/index.php/Main_Page
http://localhost/wiki/Main_Page
Ciencia Al Poder (talkcontribs)
Reply to "Missing information on some setups"

Short URL instructions for placing rewrite rule in Apache instead of .htaccess (for tech-morons like me)

1
School4schools (talkcontribs)

Took a while to figure this out, but here are my simpleton's instructions on how to place the re-write directly on Apache instead of using an .htaccess file.

My host is a Virtual Private Network (VPN) which means I have access to CPANEL and WHM

If you do not have access to WHM, this is not for you. Also, these steps are written for Windows users.


NOTE: even though the instructions here say to edit the httpd.conf file directly DO NOT EDIT IT. You will see that at the very bottom of the file it reads:

#  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#   DO NOT EDIT. AUTOMATICALLY GENERATED.  USE INCLUDE FILES IF YOU NEED TO MAKE A CHANGE

#  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

>> there is an editor function in WHM called "Include editor" I could not figure out how to use this, so I used the following steps instead. I'm sure this will drive knowledgeable users crazy, but this is how I managed to get this stupid rewrite on the server without having to use an .htacces file

>> before starting remove an existing .htaccess file and/or remove the commands for short url that may be in it.

>> we are going to give those instructions directly to the Apache server, which will be included in a file called httpd.conf We are NOT going to edit that file directly.


Step 1.

> Go to CPANEL and open File Manager

>> it should open at the /home/accountname/ directory.

>> if not, click on that directory on the left-hand menu of File Manger.

> click on "+ File" on the top menu (to add a file)

> enter a file name with the extension conf

>> any name works, but must have .conf as extension: as in filename.conf

> select that file and click on "Edit"

>> it will be blank. Add to the very top in plaint text, per instructions on this manual, this code:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d

RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]

> "Save" and "Close" the file

Step 2.

> Go to WHM and open "Terminal"

> navigate to the httpd.conf file by using "change directory" commands (cd /)

>> the file should be located on this path: /etc/apache2/conf/

>>> first, navigate to the /etc/ folder by typing cd /etc

>>> then navigate to subfolders from there by typing cd directoryname (such as cd apache2 , then cd conf)

Step 3.

> once you are in the directory /etc/apache2/conf/

>> type dir in order to see the contents of that directory

>> you should see listed there the file htttp.conf

>>>> note that the instructions manual here says it may be called apache2.conf

(<<< btw, those instructions are confusing as it indicates that apache2.conf is a directory whereas it is a file name; in my WHM, which is up to date, it is http.conf )

> now we want to read that file:

> type cat http.conf

<<< "cat" command is for "read file"

>> the file contents will spill across the terminal

>> next, highlight the file contents on the screen using your mouse (start from the bottom, hit left click & hold it while scroll up to the beginning of the file, thus highlighting all the contents)

>>> release the left click button, then right click on the selected text and select "copy"

>>> paste the contents into a plain text editor (I use notepad.exe in Windows)

>>> in the text editor, search for your website name

>>> at the bottom the entry for your website/s it will read something like this:

"  # To customize this VirtualHost use an include file at the following location

  # Include "/etc/apache2/conf.d/userdata/ssl/2_4/accountname/domain/*.conf"

( >> NOTE: if you do not have SSL enabled it that directory will read /std/)

>>> your account name and domain will be already written in that path

> copy that directory path to a new text editor window

Summary so far:

  1. we created a new .conf file using CPANEL at the root of the CPANEL File Manager and wrote the re-write code for short urls.
  2. we identified the correct path to add that file to by reading in WHM terminal, the existing httpd.conf
  3. we copied that path and file name into a text editor.

Step 4.

> type cd to go back to the terminal root directory

>> it looks like root@server.yourprimarydomain.com [~]#

> now we are going to move the file you created in CPANEL to the location that the httpd.conf file told you to place it in order for whm to read and incorporate it into the existing httpd.conf file

> we will use the move command in the terminal: mv

> in your text editor type out the entire move command with the directory paths and file name

>> it should look like this (with your particular account and domain):

mv etc/apache2/conf.d/userdata/ssl/2_4/accountname/domain/filename.conf /etc/apache2/conf.d/userdata/ssl/2_4/accountname/domain/

<< note the single space after "mv" and "filename.conf" (so it reads mv[SPACE]/path/path/filename.conf[SPACE]/path/path

> hit enter

>> this will move the file to the directory that the httpd.conf file told you corresponds to your website

>> if a file with that name is already there, it will ask you to confirm overwriting it. If so, type YES

Step 5.

> now we need to ask Apache to read the file and recompile the httpd.conf fle

> go to the root directory on the terminal

> type /scripts/ensure_vhost_includes --all-users

>> Apache will recompile the httpd.conf file and tell you if this worked or not

Step 6.

> reboot the server

> use "Gentle reboot"

Test out your site and hopefully you're good to go!


(Sadly this does not fix my issue with Visual Editor, which stopped working after I moved to short urls. That would seem to be a problem for another day)

Reply to "Short URL instructions for placing rewrite rule in Apache instead of .htaccess (for tech-morons like me)"
Proshin Rodion (talkcontribs)

1. You need to ensure the rewrite module is loaded in apache2 by doing this:

Type:

a2enmod

then when prompted type

rewrite

then press <enter>

Reply to "rewrite module"

Need some help with redirects in .htaccess

3
Summary by Berot3

didn't reply

Fredrilj (talkcontribs)

My problem is that both none HTTPS requests and requests with WWW redirects to main page, whitch is fine when the requested url is the root domain, but not if the request is an article, special page etc.

Example 1: http://mydomain.com/wiki/Articlename redirects to https://mydomain.com/wiki/Main_Page, but I want to redirect to https://mydomain.com/wiki/Articlename

Example 2: https://www.mydomain.com/wiki/Articlename redirects to https://mydomain.com/wiki/Main_Page, but I want to redirect to https://mydomain.com/wiki/Articlename

This is my settings in .htaccess:

<IfModule rewrite_module>
RewriteEngine On
Options +FollowSymlinks

# Redirect Mydomain.com to Mydomain/wiki/Main_Page
RewriteCond %{HTTP_HOST} mydomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /wiki/$1 [PT,L]

# Short url for wiki pages
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [PT,L]

# Permanent redirect www url to non-www
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule (.*) http://mydomain.com/$1 [L,R=301]

# Permanent redirect HTTP to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
</IfModule>

Any idea what I'm doing wrong?

Fredrilj (talk) 10:22, 17 April 2017 (UTC)

Robertinventor (talkcontribs)

Probably that you need to put the http to https redirect at the start rather than the end of the file - I haven't studied how .htaccess works - but if https is working fine then if you convert the http to https right at the start, the rest of the url unchanged, then as it flows through it should work just as if the user had entered an https url originally. Robertinventor (talk) 16:52, 1 October 2018 (UTC)

Stevio Bee (talkcontribs)

Make sure the 'rewrite' module is on first - had me stuck for ages.... it is off by default on Apache


'sudo a2enmod rewrite'

then restart:

'sudo systemctl restart apache2'

Reply to "Need some help with redirects in .htaccess"

Added {{Outdated}} Template to flag major cleanup

7
DanShearer (talkcontribs)

Every few years I come to this page to implement short URLs, then remember it is horribly confusing and work it out again from scratch. As many people have pointed out, the content seems authoritative but is unlikely to work on any Apache installation. In addition to being incorrect, it is also confusing. So let's start.

One of the first things is to split up Alias from Rewrite techniques into separate sections. Pretty sure the commenter here is correct in saying that Apache recommend Alias over Rewrite although I don't know a reference for that. So the Alias version should probably be listed first.

There are some working or almost-working recipes on this talk page to give us a start.

DanShearer (talk) 22:14, 18 November 2021 (UTC)

2003:DE:F743:E8FF:149C:39B2:C753:C9C9 (talkcontribs)

So it was wrong in multiple ways to blame myself for failing on this short URL task. Thank you @DanShearer for posting here and thanks even more for picking up the adventure to rewrite the page from scratch. --Paddy F. from G.

DanShearer (talkcontribs)

Thanks Paddy. I could probably make a bigger difference in a few minutes, except I don't know how to edit with the translation tags. Is there a tool that makes that easy?

Ciencia Al Poder (talkcontribs)

You can edit without adding any translation tags, and someone with knowledge will add them when you're done

DanShearer (talkcontribs)

I hardly know where to start, but I've done a bit of poking around. If someone wants to do a lot of deleting that would be good.

Dantman (talkcontribs)

Alias version should not be listed first, it shouldn't even be here at all. Alias should not be used. I've never seen the reference for Apache recommending Alias over RewriteEngine. But I have a feeling that if it exists at all it's a generic recommendation that is being mis-applied to MediaWiki.

The way Alias works is fundamentally wrong and only works because MediaWiki ignores the bad side effects that Alias introduces.

Proper rewrite rules just rewrite the path to /index.php, without any suffix or query modifications. Basically they just tell Apache, "For this url, run index.php without modifying anything". MediaWiki, not Apache, then reads the whole /wiki/Page path from the raw url in $_SERVER['REQUEST_URI'] and MediaWiki parses out the "Page" page name.

That is not how Alias works. A Alias /wiki /index.php alias actually tells apache to rewrite /wiki/Page to /index.php/Page and then passes "Page" to $_SERVER['PATH_INFO'] expecting you'll use PATH_INFO to get the "Page" title.

However, this is not actually a useful or good thing. Read the MediaWiki source code.

"PATH_INFO is mangled due to https://bugs.php.net/bug.php?id=31892" "And also by Apache 2.x, double slashes are converted to single slashes."

If MediaWiki actually used the PATH_INFO that you get by using an Alias instead of a RewriteRule, your wiki's urls could be broken.

The only reason that Alias rewrites work, is because MediaWiki outright ignores the PATH_INFO and takes responsibility for parsing the "Page" title out of the url in REQUEST_URI. i.e. It basically throws the extra stuff Alias does into the trash and demotes it to a basic RewriteEngine rewrite.

DanShearer (talkcontribs)

Dantman - ack on the reasons for deprecating Alias, which themselves need to be mentioned when deprecating it and removing recipes.

Reply to "Added {{Outdated}} Template to flag major cleanup"
Return to "Short URL/Apache" page.