.htaccess rewriterule syntax on ipage - regex

My mates and I are putting the finishing touches on our website hosted with ipage but we are just having one small issue with our .htaccess.
We want to make our urls SEO friendly (and neat) and the rewriterule is not working, we keep getting 404 errors. I just want to verify the syntax is correct. I have questioned it with ipage and they said mod_rewrite is installed and allowoverride all is set but couldn't help with the regular expression.
The url is:
http:// www.example.com/article.php?title=Some-Article
and we want to make it:
http:// www.example.com/Some-Article
the entire htaccess file:
# Protect db_connect
<files db_connect.php>
order allow,deny
deny from all
</files>
# Protect .htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
# Remove .php but still allow addressing .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]*)/$ /article.php?title=$1 [L]
Any help would be greatly appreciated as I have searched for hours and also tested the syntax generated from http://www.generateit.net/mod-rewrite/ with no luck.
Regards,
Adam

Thanks guys,
My mates and I did end up sorting it, we changed around a few things on the back-end to make the url change from:
http:// www.example.com/article.php?title=Some-Article
to:
http:// www.example.com/article.php?article=Some-Article
then with the following adjustments to the .htaccess file we got it working:
RewriteEngine On
RewriteRule page/([^/]*)/$ /index.php?p=$1 [L]
RewriteRule ^product/([^/]*)$ /article.php?article=$1 [L]
RewriteRule ^([^/]*)/$ /index.php?sort=$1 [L]
RewriteRule ^([^/]*)/page/([^/]*)$ /?sort=$1&p=$2 [L]
so we now have the following urls being formed:
http:// www.example.com/page/2
http:// www.example.com/product/Some-Product/
http:// www.example.com/
http:// www.example.com/category/page/2
The debugging really did help, appreciate it, I turned up the error reporting level in php.ini and made it output to a file which I could access through FTP.

Related

Is Apache RewriteRule interfering with If and Location directives?

I'm using Apache to implement Shibboleth single sign on for a Laravel site. I'd like to bypass one specific subset of URLs from authentication (api/public, for example) so they're publicly accessible. For some reason the public folder's .htaccess file seems to be preventing this from working as expected.
I've tried this a bunch of different ways and all roads have lead to this same issue. Here's what I'm trying currently.
<Directory /var/www/html/mysite/public>
SSLOptions +StdEnvVars
Options Indexes FollowSymLinks MultiViews
AllowOverride All
<If "%{REQUEST_URI} =~ m#api/public#">
Require all granted
</If>
<Else>
AuthType shibboleth
ShibRequestSetting requireSession 1
Require valid-user
</Else>
</Directory>
Here's the .htaccess file that Laravel ships with:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Unfortunately with the .htaccess file in place, the Else code is still getting executed and the Shib code runs. But I'm pretty sure the If condition is catching, because if throw in Require all denied as a test then it does forbid as expected.
If I remove the .htaccess however, this works! But it also means that any routing within Laravel is now broken, which I do need for this public-facing URL.
My best guess is that the .htaccess RewriteRule is causing the Else code to still run even after the If statement caught. Any suggestions on a way around this? Running into the same problem using Location directive.
Thanks.
Figured out a solution for this, will share here in case someone else runs into this. The issue was indeed that RewriteRule causes us to run through the whole thing again. The If stopped Shibboleth as expected the first time, but then we'd go through again after Laravel redirects us to index.php and pick it up anyway.
Solved it by changing the Else to an ElseIf where we exclude index.php:
<Directory /var/www/html/mysite/public>
SSLOptions +StdEnvVars
Options Indexes FollowSymLinks MultiViews
AllowOverride All
<If "%{REQUEST_URI} =~ m#api/public#">
Require all granted
</If>
<ElseIf "%{REQUEST_URI} !~ m#index\.php#">
AuthType shibboleth
ShibRequestSetting requireSession 1
Require valid-user
</ElseIf>
</Directory>

htaccess, Regex, Rewrite URL

hmm I have a problem with my .htaccess, I wanted to rewrite this:
http://domain.com/a/thread-103518.html#103518
to this
http://domain.com/a/103518
In other words... I want to delete the "thread-" and everything after the "." The /a/ must be variable... there are other forums with /bla/ and /code/
Do you have a clue for me? Thank you
My .htaccess right now:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index.php RewriteRule
^index.php/?(.*) /$1 [L,R=301]
Add this to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L,R]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Regular expression for Apache url rewrite .htaccess

I would like to rewrite this:
www.mywebsite.com/i.php?photo=1234
to
www.mywebsite.com/i/1234
Can anyone help me with the right regular expression for .htaccess
Try this:
RewriteRule ^i/([0-9]+)/?$ i.php?photo=$1 [NC,L]
Remember to turn the rewrite engine on before you declare the rule:
RewriteEngine On
The first thing you need to do is make sure Multiviews is turned off, since you have a URI that is /i/ as well as a script that is called /i.php. Multiviews and mod_negotiation will bypass mod_rewrite and your rules will get skipped.
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteRule ^i/([0-9]+)/?$ /i.php?photo=$1 [L,QSA]
And for redirecting links that are outside of your control:
RewriteCond %{THE_REQUEST} \ /+i\.php\?photo=([^&\ ]+)
RewriteRule ^ /i/%1/ [L,R=301]

static site apache and dynamic/member site django

I have a site for static content, accessible to all that runs on apache. As an adjunct to that, there is a members site that runs on django. I haven't had any issue 'sharing' my .css and making both sides equivalent in appearance, but what I can't quite seem to grok is getting my django site to be django password protected (with the additional caveat that all member material, from the login forward, goes through 443).
I can serve all the pages, I have tried to use mod_rewrite as follows:
<Directory /Library/Webserver/Documents>
.
.
.
</Directory>
WSGIScriptAlias /members /usr/local/django/mysite/apache/django.wsgi
<Directory /members>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://{HTTP_HOST}%{REQUEST_URI} [L]
.
.
</Directory>
I have tried every one of a thousand different items in the '/members location above, nothing seems to hit (and yes, RewriteEngine On is included - I can watch the debug come out).
Try:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You should be redirecting back to same host the request was made against, not necessarily the actual web server host.
I also don't believe there is a %{URI} variable in mod_rewrite. Supposed to use %{REQUEST_URI}.
EDIT 1
As per comments, should be:
<Location /members>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://{HTTP_HOST}%{REQUEST_URI} [L]
.
.
</Location>
or:
<Directory /usr/local/django/mysite/apache>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://{HTTP_HOST}%{REQUEST_URI} [L]
.
.
</Directory>
So, Location is for URL and Directory is for file system directory.

301 redirection help on wordpress

I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html