Mod-Rewrite: Need help to optimize the Rewrite Rules - mod-rewrite

Before changing something in my .htaccess links looked like this:
http://www.domain.tld/index.php
http://www.domain.tld/index.php?page=start
http://www.domain.tld/index.php?page=blog
Now I made a .htaccess with this code:
RewriteEngine on
RewriteRule ^index.php / [R,L,QSA]
Now the the links above looks like this in the browser address bar:
http://www.domain.tld
http://www.domain.tld/?page=start
http://www.domain.tld/?page=blog
That's nearly what I want, but how can I make it look like this?
http://www.domain.tld
http://www.domain.tld/start
http://www.domain.tld/blog
I tried so many different things, but mod_rewriting is not my intensity, yet.
I also want that search engines like Google uses the new URL (SEO).

Here's the solution to change links from http://www.domain.tld/index.php?page=blog to http://www.domain.tld/blog is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]
and for links like: http://www.domain.tld/index.php?page=single_news&id=1&headline=This%20Is%20A%Headline
the solution is:
RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2
After using this code, links looks like this: http://www.domain.tld/blog/2-this-is-a-headline

Related

Little tricky mod rewrite request

So this is what I currently have ruled.
RewriteRule ^([A-Za-z0-9-_]+)$ paste.php?p=$1 [L]
Url is like this.
http://website.com/pasteid
What I'm trying to do though is have
example.com/pasteid/download
OR
example.com/pasteid/plain
the GET parameters I'm passing for download / plain are a=download / a=plain
Say you would like to access your site like this:
http://website.com/pasteid/download
If I well understand you, you want this in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/(.*)$ paste.php?p=$1&a=$2 [L]
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ paste.php?p=$1 [L]
It does this:
http://www.example.com/pasteid/download => /paste.php?p=pasteid&a=download
http://www.example.com/pasteid/plain => /paste.php?p=pasteid&a=plain
The full set of rules let resource files unrewritten (css, js, png, jpg).

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

Mod rewrite for query string

Mod Rewrite noob so pardon my ignorance but all I am trying to do is a simple query string removal
from: http://yourwebsite.com/x?section=y
to: http://yourwebsite.com/x/y
I am adding my mod rewrite rules in my .htaccess like this:
ErrorDocument 404 /404
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
The problem is that on visiting:
http://yourwebsite.com/x?section=y
My rule writes it back as:
http://yourwebsite.com/x.php/y
That .php in the pretty url is pretty darn ugly and I am struggling to get rid of it.
What is wrong in my mod rewrite rule?
Most likely you are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\/(.+)$ /$1.php?section=$2 [L]
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1/%1 [R=301,L,NE]
It makes two rewrites:
a request x/y is internally rewritten to x.php?section=y
a request to x?section=y is redirected to x/y
Note that one is an internal rewrite, whilst the other redirects the browser to show a less 'ugly' url.
One hint: in case you can use the logging feature of apaches rewriting module (RewriteLog and RewriteLogLevel) this will offer you a wealth of detailed information on what is actually going on inside the rewrite process.

How can I use mod_rewrite to do this

I would like to take a url at my site:
http://mysite.com/jk/drawing
but operationally drop the "jk" dir and have my users see this:
http://mysite.com/drawing
Is this possible? If so can someone give me an example of how it is done?
thanks,
The first thing you need to do is go and change all of your links from looking like this: http://mysite.com/jk/drawing to looking like this: http://mysite.com/drawing. Without doing this, people will still see all the /jk/ URLs everywhere, the only thing you can do about it is to make sure you've changed all your links. Then add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{REQUEST_URI} !^/jk/
RewriteRule ^(.*)$ /jk/$1 [L]
In order to correct for all the links still pointing to /jk/ that you don't have any control over:
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /jk/([^\ ]*)
RewriteRule ^ /%1 [R=301,L]

How to redirect an url with parameters?

I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]

Resources