Configuring htaccess for url rewriting - apache

I am trying hard to rewrite this URL
http://localhost/sitecms/?search=viewPage&pageId=1
TO
http://localhost/sitecms/pages/page/1
N.B. Variable 'search' declared at the top inside index.php, viewPage as function declared inside index.php receives pageId variable, and pass to viewPage.php inside another folder(template) along with associated data.
Below is my .htaccess code.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pages/([^/]+)/?$ ?search=viewPage&pageId=$1 [L]
It's not working.
Suggestions are highly welcome about what I missed.

With your shown samples, could you please try following.
Please clear your browser cache before testing your URLs.
Options +FollowSymLinks
Options -MultiViews
RewriteEngine ON
##This rule redirects from http://localhost/sitecms/?search=viewPage&pageId=1 TO http://localhost/sitecms/pages/page/1
RewriteCond %{THE_REQUEST} \s/(sitecms)/\?search=viewPage&pageId=(\d+) [NC]
RewriteRule ^ /%1/pages/page/%2? [R=301,NE,L]
##Rules for handling non-existing files/directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(sitecms)/(?:[^/]*)/(?:[^/]*)/(\d+)/?$ $1/?search=viewPage&PageId=$2 [NC,L]

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

htaccess rewrite clean url for multiple parameters

This is my .htaccess code for clean url rewriting.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[a-zA-Z]{3,}\s/+index\.php\?bank=([^\s&]+) [NC]
RewriteRule ^ %1%2%3%4? [R=301,L]
RewriteRule ^([^/]+)/?$ index.php?bank=$1$2$3$4 [L,QSA]
This code works fine for single parameter in url. For example it rewrites
http://example.com/example-path/ to
http://example.com/index.php?bank=example-path/
But when I tried to pass multiple parameters in url, all I got are errors.
I need a url like
http://example.com/example-path instead of http://example.com/index.php?bank=example-path
How can I alter My code to pass multiple urls.
Your redirect logic can be greatly simplified to just:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#If a request does not match an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
#And does not match an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite all requests to index.php
RewriteRule ^(.*)/?$ index.php?bank=$1 [L,QSA]

RewriteRule in .htaccess showing rewritten url in address bar

Apologies if this has been asked before but I've searched all over for an answer to this but I haven't been able to find a satisfactory solution.
I've got the following .htaccess file in my httpdocs:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/users/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ users/$1 [L]
This correctly maps "www.example.com/joebloggs" to "www.example.com/users/jobloggs" and the appropriate index page is correctly shown.
The problem is "www.example.com/users/jobloggs" is shown in the address bar instead of the original (short) url. Would really appreciate any help on this.
If you're saying it's loading a DirectoryIndex file (e.g. index.html / index.php), then I'd say the lack of a trailing slash in your RewriteRule substitution is the problem. Try something like:
RewriteRule ^(.+?)/?$ users/$1/ [L]