1

I have some lines in .htaccess, I'm trying to flatten the URL, but it seems I'm missing something. The URL gets redirected but most of the resources are "NOT FOUND" when the URL is redirected.

Here's the code.

Options -Indexes +FollowSymLinks
RewriteEngine On
###########music#######
RewriteCond %{THE_REQUEST} \ /+profile\.php\?user=([^&]+)&title=([^&]+)&uid=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+profile\.php\?user=([^&]+)&title=([^&\ ]+)
RewriteRule ^ /%1/%2? [L,R=301]
#################internally redirect////////////
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ profile.php?user=$1&title=$2&uid=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ profile.php?user=$1&title=$2 [L,QSA]

What am I doing wrong?

7
  • 1
    All lines are commented. What URLs are not working for you? Commented Dec 3, 2014 at 6:24
  • See this question to enable rewrite trace, which may help you find what is wrong: stackoverflow.com/questions/7738170/… Commented Dec 3, 2014 at 6:28
  • @anubhava, my mistake. Please look at it again. Commented Dec 3, 2014 at 6:42
  • What URL is giving you 404 and where is this htaccess located? Commented Dec 3, 2014 at 6:44
  • @anubhava The HTACCESS is located inside the "public_html", where the index file is, which is the same directory where profile.php is located. www.site.com/profile.php?user=username&title=engineer&uid=45 redirects to www.site.com/username/engineer/45 and gives errors in the console. its a long list of files and images that can't be found. Commented Dec 3, 2014 at 6:55

1 Answer 1

1

Looks like you've hit the most common problem people face when switching to pretty URL schemes. Solution is also simple, just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

You can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.

Sign up to request clarification or add additional context in comments.

4 Comments

You saved the day. I added the <base href="/" />. That solved it.
I don't know if I should create a separate question or you can answer here. How do I preserve the the "profile" directory like on the url. Like: www.site.com/profile/username/engineer/48?
Try last rule as: RewriteRule ^profile/([^/]+)/([^/]+)/([^/]+)$ profile.php?user=$1&title=$2&uid=$3 [L,QSA]
ok that does it internally, also added RewriteRule ^ /profile/%1/%2/%3? [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.