All of my websites are generated in the following pattern:
http://www.example.com/category/xyz/article---12345.html
I want to use mod_rewrite to remove the part "---12345", where the three minuses are always there, but the amount of numbers varies from 3 to 7.
The html shoul stay there so that the urls look like that:
http://www.example.com/category/xyz/article.html
How can I achieve that?
The following rule assumes that the html file you are requesting doesn't exist and will redirect them to the new html file name. It will do this for any .html file and not just those called "article".
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{REQUEST_URI} ---
RewriteRule ^(.*)---(.*)$ "/$1.html" [R=301]
Things to note:
If the original .html file exists remove the first two RewriteCond rules.
If you don't want the address bar to change then replace [R=301] with [L]
File: .htaccess
RewriteEngine On
RewriteRule ^xyz/article---([0-9]{3,7}).html /xyz/article.html
Problem: you have to have one .htaccess in every folder where the article---xxxxxx.html is
Related
To get straight to the point.
I have this URL: http://example.com/?open=encyclopedia&letter=s&term=storm and I want this shortcut URL - http://example.com/storm - to redirect to the first URL: http://example.com/?open=encyclopedia&letter=s&term=storm
I have around 1.000 encyclopedic terms and I want this redirection to work for each term entered. For Example: if a visitor enters http://example.com/Storm to automatically be redirected to the page here: http://example.com/?open=encyclopedia&letter=s&term=storm OR http://example.com/Dried_Plant to http://example.com/?open=encyclopedia&letter=d&term=dried+plant
I prefer some htaccess solution to this, if possible.
If not, give what you can give.
I have no example code for this, since I do not know where to start from.
I suggest you redirect it all to your page encyclopedia. And then consider with php what you can do with it (like finding the first letter or change with _ or others)
You can use:
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ encyclopedia?term=%{REQUEST_URI} [NE,L]
With correct first letter (letter=), change the last line with:
RewriteRule ^(.) encyclopedia?letter=$1&term=%{REQUEST_URI} [NE,L]
#Croises answer is good if your link looks like following
mysite.com/?open=encyclopedia&letter=s&term=/storm
REQUEST_URI is adding a trailing slash. Your link don't have one:
mysite.com/?open=encyclopedia&letter=s&term=storm
I think this is what you are looking for
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&letter=%1 [NC,L]
Is it possible to use file name strings as an indicator for mod_rewrite to return pseudo directories?
I'll elaborate:
In my root directory (public_html) I have a directory named "pages" which contains the following files:
example.php
example--abc.php
instance.php
instance--2.php
Is it possible, using mod_rewrite to acheieve the following:
example.com/example/ retrieves the file of /pages/example.php
example.com/example/abc/ retrieves the file of /pages/example--abc.php
example.com/instance/ retrieves the file of /pages/instance.php
example.com/instance/2/ retrieves the file of /pages/instance--2.php
and so on...?
Thus far I have used the following with success to point /example to /pages/example.php
RewriteCond %{REQUEST_URI} !pages/
RewriteRule ^(.*)$ pages/$1.php [L,QSA]
However, it's beyond my current grasp to get the pseudo directories working defined by a double hyphen in the target's filename (--).
I've searched long and hard for a solution to no avail.
Also, the above code doesn't allow for a trailing slash (and it should, in an ideal world)
Any help much appreciated.
Thanks,
JC
Based just on description, you can try this:
UPDATED
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/pages
RewriteRule ^([^/]+)/([^/]+)/?$ pages/$1--$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/pages
RewriteRule ^([^/]+)/?$ pages/$1.php [L]
We have added a paging system inside our layout. When we go to /page/clan, the page about our clan gets displayed. (as its located in pages/clan.php).
To get /page, we used a htaccess script, which rewrites index.php?page=pagename into the /page/pagename I mentioned.
This is our current htaccess code for converting these urls:
RewriteEngine On
RewriteRule ^page/([^/]*)$ index.php?page=$1 [L]
However, We'd like to remove the /page part, so it's possible to just use /clan instead of /page/clan to open the clan page.
How can this be done and with what code?
Thanks!
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?page=$1 [L,NC]
Rewrite condions make sure you don't rewrite any existing files or directories on the server.
[NC] flag makes the pattern case insensitive, so in you case example.com/fOo would also work.
I'm sorry for this "bad" title, I'll try to explain it:
My .htaccess looks like this:
Options -MultiViews
RewriteEngine On
RewriteBase /mythbusters/tsp/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# RewriteRule ^album/(.+)$ index.php?url=album/show/$1
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The last line of this file is working perfectly. It allows to rewrite my URL from _ROOT_/index.php?url=album/index to _ROOT_/album/index (I'm using this to work with those parameters and to create beautiful URL's.
Although, there's one exception I want to make:
The out-commented line in my .htaccess file is one of my uncountable tries to rewrite a specific URL.
With the last line of my .htaccess file, the working URL looks like this:
_ROOT_/album/show/name_of_album
which is the same as
_ROOT_/index.php?url=album/show/name_of_album
Now, I want to remove the "show" part of this URL, I tried to to it with
# RewriteRule ^album/(.+)$ index.php?url=album/show/$1
But using this, every other path in my site changes which causes the CSS files fail loading (just pure HTML, no styles).
Are there some missing flags or even some wrong regex in this file?
Thanks for your help.
Chris
Have it like this:
Options -MultiViews
RewriteEngine On
RewriteBase /mythbusters/tsp/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^album/(.+)$ index.php?url=album/show/$1 [L,QSA]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
For css/js/image issue: use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can also try adding this in your page's HTML header: <base href="/" />
For example, I have an URL that looks for an image like this:
http://example.com/img/foo.png
http://example.com/img/interface/menu/bar.png
http://example.com/static/users/avatars/small/3k5jd355swrx221.jpg
I don't want to redirect those. They should just pass through. But then, I have URLs like this:
http://example.com/register/
http://example.com/my_account/my_picture/
http://example.com/contact/email/
All such URLs that don't request for an .png or .jpeg should be redirected to:
http://example.com/index.php/x
Where x stands for everything after example.com/, so in this example for example:
http://example.com/register/ to
http://example.com/index.php/register/
http://example.com/my_account/my_picture/ to
http://example.com/index.php/my_account/my_picture/
http://example.com/contact/email/ to
http://example.com/index.php/contact/email/
(AcceptPathInfo is enabled)
Is there any way to do that in the .htaccess? I only know how I could do this if I had always something like http://example.com/someKindOfMarkerHere/stuff/stuff/stuff but I don't want to have the someKindOfMarker there to detect if it's an URL that has to be rewritten. I don't know how to exclude them.
You can either exclude specific URLs:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}
Or you exclude any existing file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
Hell yes it's possible.
mod_rewrite will do all of that for you pretty easily.
You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).
The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.
Hope that helps. Just comment if you need more info from me. :)
Put something like this in a .htaccess file and make sure mod_rewrite is enabled:
RewriteEngine On
RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1
http://www.regular-expressions.info/lookaround.html
I would use a slight variation to Gumbo's answer:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.
The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]