Rewrite a javascript file url with .htaccess file - regex

I want to rewrite following url,
http://example.com/widgets/search.js?id=qerwtwttw45777
as follows,
http://example.com/widgets/search/qerwtwttw45777
and tried following rule in htaccess,
RewriteEngine on
RewriteBase /
RewriteRule search/(.*)/$ /widgets/search.js?id=$1
but it fails and showing
**500 Internal Server Error**
Anybody help to solve this problem.
Thanks

Place this code in /widgets/.htaccess:
RewriteEngine on
RewriteBase /widgets/
RewriteRule ^search/([^/]+)/?$ search.js?id=$1 [NC,L,QSA]
Make sure mod_rewrite is enabled.

Apart from the surplus slash / at the end of the pattern, the rule looks fine. So, it must be some other problem.
Maybe, mod_rewrite isn't active for your website. You could either look into the server's error log, if available or try just
RewriteEngine on
without anything else.
If it still has the 500 Internal server error, rewrite is most likely not available.

Related

Rewrite URL using .htaccess call without directory name

I really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction

How to show always the same url?

I am using Apache.
I have www.example.com/?p_action=user_profile&post_author=34 and I want to use www.example.com/niceurl/
I want that if user enters any of the urls, the www.example.com/niceurl/ is shown in his browser (of course, www.example.com/?p_action=user_profile&post_author=34 is actually retrieved from the server).
RewriteRule does not seem to be the solution for this, does it?
Thanks
Well you can try putting,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p_action=$1&post_author=$2 [L]
In your .htaccess file.
IMO RewriteRule should solve your problem.

Pattern matching for htaccess rewrite rules involving one folder to another

I need some quick help cleaning up the following rules for htaccess file, as the list is getting quite long.
This has occurred after a site move which makes author pages that previously came from /browse/author/NAME-HERE now sit at /by/NAME-HERE.
Surely there's a way to pattern match these or something?
Redirect /browse/author/some-person /by/some-person
Redirect /browse/author/another-person /by/another-person
Redirect /browse/author/some-other-person /by/some-other-person
Redirect /browse/author/yet-more /by/yet-more
Redirect /browse/author/ /
I'd like to keep the /browse/author/ redirect to the root though, if possible...
Many thanks for any help!
This is definitely possible with mod_rewrite.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^/?browse/author/(.*) /by/$1 [L,R=301,NC]

there are 2 regex rewrite but one of them is not working

Theese are my rules. First one is working but second one is not.
It was working on my old server. I Just changed my server now its not working.
RewriteRule ^oyunlar/([0-9]+)/([_0-9a-z-]+) games.php?id=$1&title=$2
RewriteRule ^post/([_0-9a-z-]+) post.php?up=$1
what could be wrong ?
on my php side, $_GET completely empty.
That is whole file;
RewriteEngine on
RewriteRule ^uye user.php
RewriteRule ^oyunlar/([0-9]+)/([_0-9a-z-]+) games.php?id=$1&title=$2
RewriteRule ^post/([_0-9a-z-]+) post.php?up=$1
RewriteRule ^tv tv.php
RewriteRule ^thumb timthumb2.php
ErrorDocument 404 /404.php
and that is the url i requested: myurl.com/post/login
i'm checking status with charles. ^post part is working but querysting part is not.
edit: i just made i a couple tests, and actually both rules not working properly and somehow, my files can work without extension like: /hello.php or /hello
additional info: this is my own private server, debian lenny + ispCP
#siniradam
Try changing your regex from this
([_0-9a-z-]+)
to this
([-_0-9a-z]+)
If a hyphen is supposed to be one of the allowed characters, it needs to be first in the list, not last.
Finally i've resolved my problem. it is occurs beacuse of MultiViews
somehow there was a rewrite and multiviews conflict. Simply i just disabled it from the .conf file.
Thank you people.

Temporary redirect 302 with .htaccess and mod-rewrite matching expression

I'm trying to match a a bunch of redirects for my website with basically moved to a different folder on the server. I need to make http://www.site.com/index.php?page=anypage go to http://www.site.com/newfolder/index.php?page=anypage. The thing is http://www.site.com/index.php and http://www.site.com/index.php?page=home should remain untouched. How can I accomplish this?
I was trying the following in the .htaccess file, but I am affraid to make a mistake. I really don't know how to test this, either.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]
Now I figured that this is temporary, so I should know ho to reverse it! The next week, the links will have to redirect again to the root server. Also, what should I do to re-establish the normal redirection??
If I've followed your scenario correctly, you want something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]
As far as testing goes, I prefer to try rules out on a local test server. If you have full control over the server (as is the case locally), there are some mod_rewrite directives that help you log what's going on, and that can be helpful in debugging. The module documentation has more information about this.
Edit: When you want to switch back, modify the RewriteRule above like so:
RewriteRule ^newfolder/index\.php /index.php [R,L]