Using .htaccess to remove unwanted parts of a URL - apache

To someone who knows what they're doing - tho will be really easy. all I want to do is remove a section of my urls, i assume that this is the easiest way.
This is how my URLs currently look:
/blog/?action=viewArticle&url=$postTile
I want to remove:
?action=viewArticle&url=
So that I end up with something like:
/blog/$postTitle
I've tried the below, but I've had no joy:
RewriteEngine on
RewriteRule ^/bwc/(blog)/(.*)/$ /bwc/blog/?action=viewArticle&url=$2
Please help - I think I need to utilise MOD_REWRITE, but I'm not too sure how.

I am not that sure but you please give a try with this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*)$ http://your_domain.com/blog/DateTimestamps-of-news-posts$1 [R=301,L]
See what comes with... :)

Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /site/blog/\?action=viewArticle&url=([^&\ ]+)
RewriteRule ^ /site/blog/%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^site/blog/([^/])+$ /site/blog/?action=viewArticle&url=$1 [L,QSA]
These rules need to be in the htaccess file in your document root.

Related

Simple RewriteRule doesn't change URL

The thing I ask for is truly simple. I want to get from this:
http://test.com/index.php?articles=mynewarticle&id=5
to this:
http://test.com/mynewarticle.html
and I don't understand why, but the following .htaccess is not doing the job:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /index.php?articles=$1&id=5 [L]
When I type in
http://test.com/index.php?articles=mynewarticle&id=5
I get
http://test.com/index.php?articles=mynewarticle&id=5
Not sure if something is modified in Apache in the way that disables this option for me and I don't know where to check this. Thanks in advance for any of the help provided.
You need a combination of external and internal RewriteRules to convert orignal url into SEO friendly format
RewriteEngine on
#redirect "/index.php?articles=foobar&id=123" to "/foobar.html"
RewriteCond %{THE_REQUEST} /index\.php\?articles=([^&]+)&id=.+ [NC]
RewriteRule ^ /%1.html [NE,L,R]
#rewrite "/foobar.html" to "/index.php?articles=foobar&id=5
RewriteRule ^([^/]*)\.html$ /index.php?articles=$1&id=5 [L]

Unexpected behaviour of rewriterules in htaccess

This is my htaccess file
RewriteEngine on
rewriterule ^([a-zA-Z0-9]+) /vixer.php?v=$1 [QSA]
the main problem is that when I enter www.example.com/vix_25 it goes to 404
and when I enter another url /vix25 it works.. I really dont understand what is the diffrence and why 1st doesnt work.
Can you guys help me? thanks
You need to add an underscore to the pattern so that it can match a "_" in the Requested URI
([a-zA-Z0-9_]+)
Try this :
RewriteEngine on
rewriterule ^([a-zA-Z0-9_]+)$ /vixer.php?v=$1 [QSA,L]
I would make it a bit more robust for your rule. Tell apache that if it's not a real file and not a real directory then rewrite it. This prevents 404's too. I also changed the regex so that it will match better instead of individually adding matches. Also I would make sure MultiViews are off.
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
rewriterule ^([^/]+)/?$ /vixer.php?v=$1 [QSA,L]

Triming URL with .htaccess

I've been reading a lot about rewriting but simply I'cant make it done the right way.
So my question is how can I with .htacces and rewrite rule make this link :
www.website.com/?section=rijecnik
look like this
www.website/rijecnik
Now I'm using this rule but it doesn't work
RewriteEngine On
RewriteRule ^/([A-Za-z0-9]+)$ ?section=$i
Can anyone explain to me what am I doing wrong ?
You have to remove leading slash after RewriteRule.
Your code should look like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /?section=$1 [L]

Rewrite URLs with .htaccess but ignore specific Directories

I have a directory on a site:
http://example.com/directory/
In it I have a .htaccess file.
I want it to take any URL like this:
http://example.com/directory/section/day/
And rewrite it to:
http://example.com/directory/index.php?arg1=section&arg2=day
Except for any URLs that refer to these directories:
http://example.com/directory/css/
http://example.com/directory/javascript/
http://example.com/directory/images/
I have the first part working, but unable to tell it to exclude files in certain directories:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
Update:
This works in a very basic and simple sense:
RewriteRule ^css - [L,NC]
RewriteRule ^javascript - [L,NC]
RewriteRule ^images - [L,NC]
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
but I'm sure there is a more elegant solution?
I think the keyword you're looking for is RewriteCond. It's pretty similar to what you ended up with.
RewriteCond %{REQUEST_URI} !^(css|javascript|images)
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
You might be able to use RewriteCond to check if an actual file exists... at least, I use this for websites when I want things like CSS, Javascripts, and images to be accessible (without the request being redirected.)
Here's the line I use:
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
Let me know if that works for you!

Help with mod_rewrite rule for dynamic url

Ugh.. mod_rewrite makes me feel stupid. I just haven't wrapped my brain around it yet. :/
I have this url:
http://example.com/a/name/
...that I want to point here:
http://example.com/a/index.php?id=name
...where name is what is getting passed to index.php as the id argument.
Anything I've tried results in either a 404 or a 500.. :(
If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.
But if the trailing slash is mandatory, there is no need to exclude the destination file:
RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
To start you off:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
If one rewrite tutorial doesn't work for you, try another.
Edit: excluded index.php as per Gumbo's suggestion
Maybe something along the lines of
RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
would do the trick.
I suggest you take a look at this URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/
The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.