simple url rewriting problem - iis

I have an existing website where i have urls like:
/application/index/fuseaction/home.uebersicht/a/44/b/45/web/xyz.htm
I now want to redirect to:
/xyz.html
what it tried is
RewriteCond %{HTTP:Host} ^(?:www\.)?mysite.coml$
RewriteCond %{REQUEST_URI} application/index\.cfm.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteMap docmap_301 txt: mapping301.txt
RewriteRule ^/application/index.\cfm/(.*)\.htm ${docmap_301:$1} [NC,L,NS]

If this is an inbound rule then this should work for you:
Rewrite Engine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/((.+).htm) $2l [NC,R=302]
I'm assuming that the missing l from the long url in your example was intentional.

Related

Use 2 urls for the same directory using htaccess

Is it possible to using htaccess?
I want to keep my original url "menu", but also use /en/menu/ for it.
Here is my code so far (sorry I am terrible at htaccess):
RewriteEngine On
RewriteBase /server/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/menu/ [NC]
RewriteRule ^([^/]+)/([^/]+)en/menu/ [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /menu/ [L,QSA]

Transfert an url with parameters into a pretty URL with htaccess

I want to use URL rewrite with my .htaccess to redirect my non pretty URL:
http://example.com/_new/url.php?module=planning&action=see&id=2
to this one:
http://example.com/_new/url/planning/see/2
So I used:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ url.php?module=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ url.php?module=$1&action=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ url.php?module=$1&action=$2&id=$3 [L]
But it doesn't work.
For this specific url rewrite, You might need to replace your [RewriteRule] to something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^_new/url/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$
_new/url.php?module=$1&action=$2&id=$3 [NC,L]
From which you can access: http://example.com/_new/url.php?module=planning&action=see&id=2
http://example.com/_new/url/{module_name} = http://example.com/_new/url.php?module={module_name}
The same applies for $2, $3 as well.
[NC] = Case insensitive : This flag tells apache that there will be [no case]
[L] = If this rule was already processed to begin with, No additional rules will be added in the current url rewriting process
You can learn more about here: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Hope this helps.

Using an IF statement in a HTACCESS ReWrite rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]

htaccess redirect if URI contains string to index.php

I would like to redirect link http://example.com/asdasdas/edit to http://example.com/asdasdas/index.php/edit using htaccess. I did this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} edit
RewriteRule ^ %{REQUEST_URI}/index.php [L,R=301]
but it's a loop and doesn't works well. Any ideas?
You can use this rule in /asdasdas/.htaccess:
RewriteEngine On
RewriteBase /asdasdas/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php/$ [L]
This will support URL of http://example.com/asdasdas/edit

Is there a way via htaccess to change URLs to clean URLs by deleting a parat of an URL?

My all URLs formats are like these:
http://example.com/category?format=feed
http://example.com/category/sub_category/?format=feed
for example: http://example.com/computer/cpu/?format=feed
Is there a way via htaccess to change top URLs to these:
http://example.com/category/feed
http://example.com/category/sub_category/feed
example1: http://example.com/computer/cpu/feed
example2: http://example.com/computer/feed
These rules should meet both requirements. But allows you to change the category and subcat as long as the normal URLs don't change and format is always a key in the query string.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2/?format=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/?format=$2 [L]
Then you should be able to use these URL types.
http://example.com/category/feed
http://example.com/category/sub_category/feed
The following should achieve what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)/(.*)/$ /$1/$2/%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)$ /$1/%1? [L,R]
The above will turn:
http://example.com/category?format=feed to http://example.com/category/feed
and
http://example.com/category/sub_category/?format=feed to http://example.com/category/sub_category/feed

Resources