Remove number from URL - .htaccess

I am trying to remove some directories and the number from the URL.
I can remove the DIRs but not the number.
Existing URL: domain.com/enews/item/1765-septic-shock-nothing-is-black-and-white
New URL domain.com/septic-shock-nothing-is-black-and-white
This was my attempt, but it is not removing the number:
RewriteEngine On
RewriteRule ^enews/item/[0-9](.*)$ /$1 [L,R=301]

Figured it out. You need to use this:
RewriteRule ^enews/item/[0-9]+-(.*)$ /$1 [L,R=301]

Related

htaccess - adding prefix to new url

how am I suppose to add a prefix to new url?
RewriteEngine on
RewriteRule ^(.*) http://localhost/redirect/s_$1 [R=301,L]
I want it to redirect to new url with s_ prefix example s_about.html
but it it redirects me to this
http://localhost/redirect/s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_index.html
Try with below rule we are skipping urls which have s_ prefix.
RewriteEngine on
RewriteRule ^\b(?!s_)(\w+)$ http://localhost/redirect/s_$1 [R=301,L]

Htaccess redirect remove part of url if contains certain string

How can I change my url from /images/hold/... to /hold/...
I have this but it doesnt work:
RewriteRule ^/images/hold/(.*) /hold/$1 [R=301,NC,L]
RewriteRule ^/images/hold/(.*) /hold/$1 [R=301,NC,L]
In per-directory .htaccess files the URL-path that is matched by the RewriteRule directive never starts with a slash. So, try the following instead:
RewriteRule ^images/hold/(.*) /hold/$1 [R=301,NC,L]
Or, to cut out some repetition:
RewriteRule ^images/(hold/.*) /$1 [R=301,NC,L]
If you've been experimenting with 301 redirects then you may need to clear your browser cache, as these get cached hard by the browser.

.htaccess Rewrite Filename as Page

I'm trying to have my .htaccess file rewrite a URL with this ending:
/Management_Agreement_fillable.pdf
as:
/?page=viewer&viewer=G&fileID=1
However, when I try the following:
RewriteCond ^/Management_Agreement_fillable.pdf$
RewriteRule (.*) /?page=viewer&viewer=G&fileID=1 [R=301,L]
It doesn't work. What should I be doing?
You don't need the condition:
RewriteRule ^Management_Agreement_fillable\.pdf$ /?page=viewer&viewer=G&fileID=1 [R=301,L]
If you don't want the URL in the address bar to change, remove the R=301, from the square brackets.

301 Redirecting Multiple Pages

My rewrite rule almost works but there are still some problems. Here is the code:
RewriteEngine on
RewriteRule ^tag/(.*)$ /?s=$1&search=GA [L,R=301]
The first problem now is that the redirect links to:
mydomain.com/?s=tag/&search=GA
How can I get rid of the second slash?
Now the second problem... when a tag contains more than 1 word (for example the tag marketing tips) the redirect is:
mydomain.com/?s=marketing-tips/&search=GA
How do I convert that - symbol to a + symbol?
Try this:
# getting rid of trailing slash:
RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L]
# change "-" with "+":
RewriteCond %{QUERY_STRING} ^s=([^&]+)-([^&]+)&(.*)
RewriteRule ^(.*)$ /$1?s=%1+%2&%3 [L,NE]
# if there's no more "-", redirect:
RewriteCond %{QUERY_STRING} ^s=([^&-]+)(&|$)
RewriteRule ^(.*)$ /$1 [L,R=301]
When I go to a URL like mydomain.com/tag/some-thing-else-lots-of-dashes/, I get redirected to mydomain.com/s=some+thing+else+lots+of+dashes&search=GA
It looks like your regex is picking up the slash from the incoming URL in it's collection, try moving it out and making it optional, also I made your selector less greedy:
RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L,R=301]

Remove subdirectory & remove .php extention in htaccess not working?

I m new to url rewrite:
First, here is what I am trying to accomplish:
Current URL: www.example.com/subdirectory1/subdirectory2/something.php
Desired URL: www.example.com/subdirectory1/something/
And, the name of subdirectory2 is fixed.
Possible?
My current htaccess just to remove the ".php" but also not working. (Any idea how to debug htaccess??)
RewritEngine on
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(?!subdirectory1/|subdirectory2/)(.+)$ subdirectory1/$1 [L]
Thanks.
Your first problem is RewritEngine on. You are missing an e. Should be RewriteEngine on.
Try this:
RewriteEngine on
# Remove .php
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^([^/]+)/fixed/([^/]+).php$ /$1/$2/ [R=301,L]
# Rewrite "friendly" URL into php.
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/fixed/$2.php [L]
This only works for exactly what you said. Fixed is always the same. Replace it with the correct value.
The users goes to: www.example.com/1234/fixed/5678.php. He is redirected to www.example.com/1234/5678
User goes to www.example.com/1234/5678. On the server, this becomes www.example.com/1234/fixed/5678.php.
Something like www.example.com/1234/5678/9abcd will not work. (More than two levels of directories.)

Resources